Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

C# Spawn one enemy type at a time?

Discussion in 'Scripting' started by clearrose, Aug 6, 2015.

  1. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    Hello unity community, we have three spawners spawning three different kinds of nets. If the player comes in contact with the nets he gets captured. Each net has different traits, however we only want one type of net at a time to appear.

    Our, code to achieve this doesn't seem to be doing the trick:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NetSpawner3 : MonoBehaviour {
    5.  
    6.     public GameObject Instance;                // The enemy prefab to be spawned.
    7.     public GameObject OtherNet1;
    8.     public GameObject OtherNet2;
    9.     public float maxtime = 25;           // How long between each spawn.
    10.     public float mintime = 10;
    11.    
    12.     //current time
    13.     private float time;
    14.    
    15.     //The time to spawn the object
    16.     private float spawnTime;
    17.    
    18.     public Transform[] spawnPoints;         // An array of the spawn points this enemy can spawn from.
    19.    
    20.     void Start ()
    21.     {
    22.         SetRandomTime();
    23.         time = mintime;
    24.        
    25.     }
    26.    
    27.     void FixedUpdate(){
    28.        
    29.         //Counts up
    30.         time += Time.deltaTime;
    31.        
    32.         //Check if its the right time to spawn the object
    33.         if(time >= spawnTime){
    34.             Spawn();
    35.             SetRandomTime();
    36.         }
    37.        
    38.     }
    39.    
    40.     public void Spawn ()
    41.     {
    42.         if (!OtherNet1)
    43.         {
    44.             time = 0;
    45.    
    46.             GameObject obj = NewObjectPool.instance.GetObjectForType ("NetOfDeathV3 3", true);
    47.             if (obj == null)
    48.                 return;
    49.    
    50.             obj.transform.position = transform.position;
    51.             obj.transform.rotation = transform.rotation;
    52.             obj.SetActive (true);
    53.         }
    54.         else if (OtherNet1)
    55.         {
    56.             StopSpawning ();
    57.             print("No net3's are coming");
    58.         }
    59.         if (!OtherNet2)
    60.         {
    61.             time = 0;
    62.            
    63.             GameObject obj = NewObjectPool.instance.GetObjectForType ("NetOfDeathV3 3", true);
    64.             if (obj == null)
    65.                 return;
    66.            
    67.             obj.transform.position = transform.position;
    68.             obj.transform.rotation = transform.rotation;
    69.             obj.SetActive (true);
    70.         }
    71.  
    72.         else if (OtherNet2)
    73.         {
    74.             StopSpawning ();
    75.             print("No net3's are coming");
    76.         }
    77.     }
    78.  
    79.     public void StopSpawning(){
    80.  
    81.         CancelInvoke("Spawn");
    82.     }
    83.     //Sets the random time between minTime and maxTime
    84.    
    85.     void SetRandomTime(){
    86.         spawnTime = Random.Range(mintime, maxtime);
    87.     }
    88. }
    89.  
     
  2. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    Your if statments are a bit botched up...

    if(!OtherNet1)..... OtherNet1 is a GameObject so you are effectively saying "if OtherNet1 is null then do this" if you don't have anything assigned in the inspector as OtherNet1 this code will always run.... same for the check for OtherNet2. If you do have something assigned... it cancels the spawn.

    What do you want to happen? Spawn net 1... wait 15-25 sec... spawn net 2... wait 15-25 sec... spawn net 3... wait and repeat?
     
  3. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    Oh oops, that is not what we wanted at all. We wanted to have all three nets spawn at random, however check to see if another net of another type is already spawned. If so don't spawn, until that net is false.
     
  4. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    so instead of instantiating object you always have same object and change its appearance and stats? if i get that correctly?
    also why not just random range or value and check for which web to spawn, this code is not entirely clear to me, as this code checks if there is web 1, if not then spawn and if it does then stops spawning, it can never reach web 2 and also there is no sign of web 3 (or you excluded that on purpose)
     
  5. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    Note: We have there spawners because we had difficulty with just one spawner. With one spawner the net would hit the ground and would stay there. Since our background is scrolling it made the net look like it was moving backwards. So to fix this we had to animate the nets within unity, but the animation had overwritten the spawners transform. Therefore the nets were always coming out at the same spot on screen no matter where the spawner was. So we animated three nets in certain positions to cover the screen area, thus each net got it's own spawner. However to make them random we used a random range with a min and max time. Now the only problem we are having is that we want one net at a time to spawn.

    No, We have three different spawners one for each net that has different settings.

    Since there are three different spawners, I want the spawners to check to see if the other spawner has spawned a net. If so don't spawn until that net is false.

    Oh how do I fix it, I want to reach net 2.

    I didn't include net 3 because this is the spawner code for net 3, I want to spawn when the other two are false.
     
  6. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    By this script you only set WHEN to spawn not WHAT to spawn so it will always spawn at spawner one. You need to send int to spawn.
    First call spawn like this
    Code (CSharp):
    1. Spawn(Random.Range(0,4);
    Then with if else statment or switch to determine which to spawn. 1, 2 or continue to 3 (i still dont know how you spawn 3 as it calls stopspawning.

    This would be alot easier with manager than spawner 3. If you want to spawn only one wrb you can create public GameObject variable and store current wrb at it and when ready to spawn new, set that gameobject variable to null.
     
  7. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    Sorry I'm not as advanced as you, so I don't understand.
     
  8. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    This is unconvencional way of doing things. You probably encountered one smaller problem and as workaround you created much harder path. You gave too little info about your script for me to understand how this work. What i got is that in net3 script you check if 1 and 2 are spawned and you want if neither is spawned to spawn 3 but you ended up with spawning 3 if 1 is not spawned. There is no of explanation where rest of process is.

    Easiest way is that 1 spawner with script spawns all webs and controls when and where they are spawned and web behaviour is controlled in their script (also movement can be controlled in their script). Your method sounds way more coplicated.
     
  9. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    if you like your set up and just want to cycle through the nets randomly...

    Code (csharp):
    1.  
    2. public GameObject[] nets = new GameObject[3];
    3.     public float minTime = 10.0f;
    4.     public float maxTime = 25.0f;
    5.  
    6.     private int curNet = 0;
    7.  
    8.  
    9.     void Start(){
    10.         Invoke("Spawn", Random.Range(minTime,maxTime));
    11.     }
    12.  
    13.  
    14.     void Spawn(){
    15.         //just copied your code here
    16.          GameObject obj = NewObjectPool.instance.GetObjectForType (nets[curNet].name, true); // had "NetOfDeathV3 3" .. also assuming the prefabs have the correct names
    17.             if (obj == null)
    18.                 return;
    19.  
    20.             obj.transform.position = transform.position;
    21.             obj.transform.rotation = transform.rotation;
    22.             obj.SetActive (true);
    23.  
    24.         curNet++;
    25.  
    26.         if(curNet > nets.Count){
    27.             curNets = 0;
    28.         }
    29.  
    30.         Invoke("Spawn", Random.Range(minTime,maxTime));
    31.     }
    32.  
    33. [\code]
     
    Last edited: Aug 7, 2015
  10. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    Sorry :( for everything, I think I might just go ahead and try one spawner.

    Thank you very much, but when I tried this I got errors:

     
  11. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    post your code now. Also, I looked through what I had and seem a typo... curNet was supposed to be type int but was set as interface. it has been fixed above. That's what I get I guess for opening VS to just cobble a script together.
     
  12. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    still got errors on line 30, and 31. I assume that maybe there is a missing variable for count?

    Was I suppose to add this to my existing code? or just try it? sorry for being a noob......

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NetspawnerNew : MonoBehaviour {
    5.    
    6.     public GameObject[] nets = new GameObject[3];
    7.     public float minTime = 10.0f;
    8.     public float maxTime = 25.0f;
    9.    
    10.     private int curNet = 0;
    11.    
    12.    
    13.     void Start(){
    14.         Invoke("Spawn", Random.Range(minTime,maxTime));
    15.     }
    16.    
    17.    
    18.     void Spawn(){
    19.         //just copied your code here
    20.         GameObject obj = NewObjectPool.instance.GetObjectForType (nets[curNet].name, true); // had "NetOfDeathV3 3" .. also assuming the prefabs have the correct names
    21.         if (obj == null)
    22.             return;
    23.        
    24.         obj.transform.position = transform.position;
    25.         obj.transform.rotation = transform.rotation;
    26.         obj.SetActive (true);
    27.        
    28.         curNet++;
    29.        
    30.         if(curNet > nets.Count){
    31.             curNets = 0;
    32.         }
    33.        
    34.         Invoke("Spawn", Random.Range(minTime,maxTime));
    35.     }
    36. }
    And the errors:

     
  13. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    curNet not curNet at last if statment
    also make just public GameObject[] nets; without new definition. and assign gameobjects in editor.
    try with nets.length instead of count. if none works just put if > 2. it should fix you that script, still it is not random

    if you that works you can easily adjust it to be random by changing curNet++ to curNet = Random.Range(0,3)
     
  14. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    I actually had a script I posted a couple of weeks ago that did this- it might be of assistance to you as it was very heavily commented (made it as an example for someone). Let me just go see if I can locate it...

    Edit: Found it, here you go.
     
  15. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    Thanks very much this did work :) , and sorry for the delayed response.

    Well, it is nice to have a alternative thanks ^_^ , for the help.
     
  16. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    Well, now we are having a different problem with our spawner. Even though we set the variables for minTime and maxTime, the instances seem to have a mind of their own. We would set for example, 35 for the minTine and maxim to 65 and the Instance would spawn almost instantly on start.Help us :( .....
     
  17. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    You probably accidentally overwrote minTime in the Inspector. Keep in mind that no matter what value you set as a default in the script, the Inspector is going to wipe it out.
     
  18. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    Yeah, I know the times in the inspector are fine.