Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Enable game objects after x amount of time

Discussion in 'Scripting' started by Kiesco91, Mar 26, 2020.

  1. Kiesco91

    Kiesco91

    Joined:
    Aug 20, 2018
    Posts:
    80
    Hi all, so I have a game with 20 infected enemies but I want them to appear in order one after another at different times. (infected A enables at 10 seconds, infected B enables at 20 etc etc)

    I have tried different codes and its driving me insane. if someone could help, that would be great. I know my code below is only set up to enable the first infected but I don't think I can do this 19 times for the rest or even if that would work. I don't want to resort to an individual script to each enemy!

    my base code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Spawner : MonoBehaviour
    6. {
    7.     public GameObject infected1;
    8.     public GameObject infected2;
    9.     public GameObject infected3;
    10.     public GameObject infected4;
    11.     public GameObject infected5;
    12.     public GameObject infected6;
    13.     public GameObject infected7;
    14.     public GameObject infected8;
    15.     public GameObject infected9;
    16.     public GameObject infected10;
    17.     public GameObject infected11;
    18.     public GameObject infected12;
    19.     public GameObject infected13;
    20.     public GameObject infected14;
    21.     public GameObject infected15;
    22.     public GameObject infected16;
    23.     public GameObject infected17;
    24.     public GameObject infected18;
    25.     public GameObject infected19;
    26.     public GameObject infected20;
    27.  
    28.  
    29.     void Start()
    30.     {
    31.         StartCoroutine(enableInfecteda(v: 30));
    32.  
    33.     }
    34.  
    35.     private IEnumerator enableInfecteda(int v)
    36.     {
    37.         infected1.SetActive(false);
    38.         yield return new WaitForSeconds(30);
    39.         infected1.SetActive(true);
    40.     }
    41.  
    42.  
    43.  
    44.  
    45. }
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Use an array to store your gameobjects. Then keep track of the index you are at, starting at 0.
    Now run some code which enables the infected at the tracked index, and resets the timer again. This can be done by a coroutine, or since i'm not a big fan of those, with a timer in the update loop. So first you would enable infecteds[0], then infecteds[1] and so on, each with x-amount of seconds between. The number would of course not be fixed 0 or 1, but saved in an integer 'spawnInfectedIndex', which you increment each time you spawn a new infected.

    I'm leaning outside the window to say that your infecteds themselves are probably not unique, but rather mostly copies (other than maybe their position or health values). If so, instead of saving an array, you could just save the prefab and instantiate it. This is the normal and prefered way of 'spawning' enemies.

    This may seem like a bit of a lackluster explanation, but if that's the case really need to look into things like arrays first. They are fundamental building blocks of any program. Same goes for loops. And in case of unity, prefabs and so on.

    Hope this helps :)
     
  3. Kiesco91

    Kiesco91

    Joined:
    Aug 20, 2018
    Posts:
    80
    Thank you for your response.

    I have tried with an array but couldn't get the index to work with anything and that's why I went with the long slow process of adding each one. All tutorials online have random spawn points or times and not a fixed spawn on a fixed timer.

    Don't want to be cheeky and Im not going to expect you to write it all out for me but is there any chance of maybe the fundamentals as my brain is frazzled from trying.

    It's cool if not, I'll have another go to tomorrow but I've wasted so much time on simple things today haha
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Code (CSharp):
    1. // In some script
    2.  
    3. [SerializeField] private GameObject infectedPrefab = null; // assign through inspector
    4. [SerializeField] private Transform spawnLocation = null; // assign through inspector
    5.  
    6. [SerializeField] private float spawnTimeInterval = 30f;
    7. private float lastSpawnTime = 0;
    8.  
    9. // If you want spawning to happen 'exactly' after 30s, set lastSpawnTime to Time.time in Start()
    10. // If you want want the first spawn to happen 'instantly', set lastSpawnTime to minus spawnTimeInterval in Start()
    11.  
    12. void Update(){
    13.     if(Time.time - lastSpawnTime > spawnTimeInterval){
    14.         lastSpawnTime = Time.time;
    15.         GameObject newInfected = Instantiate(infectedPrefab, spawnLocation.position, Quaternion.identity);
    16.     }
    17. }
    Except some typos, this should work. Put your infected prefab into the infectedPrefab variable through the inspector, create some empty object which is supposed to be your spawn location and assign it to spawnLocation in the same way. You can also adjust spawnTimeInterval if you so desire. Now we simply check if spawnTimeInterval-many seconds passed since the last spawn, the moment of which we save in lastSpawnTime and update once we spawn a new infected.

    That said, random spawns on a random timer is just a more advanced system than fixed spawns on a fixed timer. You should be able to extract the idea from these videos as well. If you do not feel confident enough yet, i would recommend you looking into more general purpose tutorials. Sebastian Lague did a good series on C# + Unity game development on youtube.

    Hope this helps.
     
  5. Kiesco91

    Kiesco91

    Joined:
    Aug 20, 2018
    Posts:
    80
    Update!

    I finally have this solved! thank you for your help!

    onto fixing my next problem :( haha
     
    Kurt-Dekker likes this.
  6. anchiyis

    anchiyis

    Joined:
    Oct 7, 2022
    Posts:
    1
    Hi, how did you solve this? I have the same case but still couldn't solve it. :(