repeating code every __ seconds

 lets say you want to spawn a gameobject every 3 seconds.

First you'll need a Spawner object. It could be anything - lets use an Empty. Place it wherever you want.


Make a new C# script and apply it to the Empty. Here's the code, with some comments


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class spawner : MonoBehaviour
{
    public GameObject thing; //the object you want to spawn. Set it in the Inspector

   
    void Start()
    {
        InvokeRepeating("makeThing", 3.0f, 1.0f); //calls makeThing function after 3 secs, every 1 sec
    }


    void makeThing() //the actual spawn script
    {
        
        Instantiate(thing, transform.position, Random.rotation); //uses object "thing", at your Empty's position, with a random rotation//


    }



}




https://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html

Comments

Popular posts from this blog

setting VFX graph properties using C#