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

Not instantiating more than one gameobject at once, no idea why

Discussion in 'Scripting' started by Nicholas1010, May 2, 2016.

  1. Nicholas1010

    Nicholas1010

    Joined:
    Aug 2, 2014
    Posts:
    20
    Hello, I have a little messy code, so sorry about that (which is one of the reasons I am probably getting this bug)

    Basically I made a script, attached it to gameobject, and it instantiates/spawns my prefab, but no matter what variable I put in InvokeRepeating in its function, I still get fixed instantiating, basically there are circles falling down in my game and I want to control how many circles spawn and how often etc. I managed to do this before but something went wrong in this particular script, here are notable spots in script:

    in Start:
    Code (csharp):
    1. InvokeRepeating("SpawnCooldownChange", spawnCooldownFreq, spawnCooldownFreq); // No matter what I put here in variable, it does not actually change, even though in inspector it is changed
    2. InvokeRepeating("SpawnObjectiveCircle", 0.1f, baseSpawnRepeat);
    in void SpawnObjectiveCircle():

    Code (csharp):
    1.  
    2.     void SpawnObjectiveCircle()
    3.     {
    4.         GameObject newGameObject;
    5.         newGameObject = (GameObject)Instantiate(circles[Random.Range(0, 5)], defaultSpawn + new Vector3(0, 8, 0), transform.rotation);
    6.         newGameObject.transform.parent = this.transform;
    7.         list.Add(newGameObject);
    8.         GravityForce(); // give gravity
    9.         InvokeRepeating("DestroyOutOfView", 4, 3.0F);
    10. ...
    in void DestroyOutOfView():

    Code (csharp):
    1.  
    2.  void DestroyOutOfView() //It basically despawns everything that leaves screen.
    3.     {
    4.         foreach (GameObject CirclesObject in list)
    5.         {
    6.             if (CirclesObject.gameObject.transform.position.y <= -8)
    7.             {
    8.                 CirclesObject.gameObject.SetActive(false);
    9.             }
    10.         }
    11.     }
    And this one is quite important one and probably cause of a bug, in void SpawnCooldownChange():
    Code (csharp):
    1.  
    2. void SpawnCooldownChange()
    3.     {
    4.         if (baseSpawnRepeat > 2.7)
    5.         {
    6.             spawnCooldownFreq = 0.7f;
    7.             baseSpawnRepeat -= 0.08f;
    8.         }
    9.         else if (baseSpawnRepeat < 2.7 && baseSpawnRepeat > 1.6)
    10.         {
    11.             spawnCooldownFreq = 0.5f;
    12.             baseSpawnRepeat -= 0.16f;
    13.         }
    14.         else if (baseSpawnRepeat < 2.7 && baseSpawnRepeat > 0.7)
    15.         {
    16.             spawnCooldownFreq = 0.4f;
    17.             baseSpawnRepeat -= 0.18f;
    18.         }
    19.         else if (baseSpawnRepeat < 2.7 && baseSpawnRepeat > 0.25f) // FOR SOME REASON SAME ISSUE AGAIN, NO MATTER WHAT I GIVE, IT DOES 1 CIRCLE AT A TIME.
    20.         {
    21.             spawnCooldownFreq = 0.4f;
    22.             baseSpawnRepeat -= 0.16f;
    23.         }
    24.     }
    ^^ Here I just adjust how fast/often will they spawn

    So problem is, it actually waits like 5 seconds and then instantiates instead of baseSpawnRepeat variable.

    I posted this kinda in a hurry so please do tell me if I made any mistakes or forgot something.

    I guess I did something really stupid so, help would be greatly appreciated!
     
  2. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    Just to check, but the object where the InvokeRepeating is, does it get deleted or marked inactive during play? Those are the only cases I can think of that would cause InvokeRepeating to stop. Also, I suspect that InvokeRepeating, once called, does not update the variables and they get locked in.
    I could be wrong, but that's what I suspect is going on.
     
  3. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Your can't change the frequency of the invokes in an InvokeRepeating. It is permanently set to the value set while calling it.
    You also seem to invoke DestroyOutOfView in sort of a recursive manner, repeating the invoke for each SpawnObjectiveCircle, leaving you with more and more repeating invokes as time goes by.
    I suggest looking into coroutines where you can dynamically adjust the delays.
     
  4. Nicholas1010

    Nicholas1010

    Joined:
    Aug 2, 2014
    Posts:
    20
    Hello, sorry for late bump.

    I tried adapting my code to make it use StartCoroutine, but now everything seems to be messed up.

    I think it's because my coroutine only gets called once even though I do "yield return new WaitForSeconds(baseSpawnRepeat);"

    Any idea what might be going wrong? I probably made some stupid mistake.
     
  5. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    You need to loop within your coroutine.
    Here's an example:
    Code (csharp):
    1.  
    2. public class LoopRoutineForever : Monobehaviour
    3. {
    4.     private void Start()
    5.     {
    6.         StartCoroutine(LoopForever());
    7.     }
    8.     private IEnumerator LoopForever()
    9.     {
    10.         // Loop forever while application is running
    11.         while (Application.isPlaying)  
    12.         {
    13.                 Debug.Log("Begin loop.");
    14.             yield return new WaitForSeconds(1f);    // Wait a second.
    15.                 Debug.Log("Just waited a second, will wait one more...");
    16.             yield return new WaitForSeconds(1f);    // Wait a second.
    17.                 Debug.Log("End loop.");
    18.             yield return null;    // Wait one frame
    19.         }
    20.     }
    21. }
    22.  
     
    Nicholas1010 likes this.
  6. Nicholas1010

    Nicholas1010

    Joined:
    Aug 2, 2014
    Posts:
    20
    Thanks for reply! That example helped me fix my problem, its all good now.
     
    ThermalFusion likes this.