c# - Why does the GameObject not append to the List? -


i working on game prototype, game require spawn gameobjects call "stars", instantiating, when try delete them when there many around not work, trying put instantiated gameobjects in list , delete last object when new 1 instantiated. can see code, when 3 objects spawned script should delete 1 beginning , spawn new 1 in same time.

now, problem don't know missing here, code not work , don't know why. please me. thank you!

here code:

using system.collections; using system.collections.generic; using unityengine;  public class spawnstar : monobehaviour {      public float addedspeed = -200f;      private gameobject spawned;     private float randomx;     public gameobject startospawn;     private list<gameobject> activego;       void start ()     {         activego = new list<gameobject> ();         invoke ("instantiatestar", 2f);     }      // update called once per frame     void fixedupdate ()      {         getcomponent<rigidbody>().addforce( new vector3( 0f, addedspeed, 0f));     }      void instantiatestar ()      {         randomx = random.range (-3f, 3f);         gameobject go;         go = instantiate (startospawn, new vector3(randomx, 5f, 0f), transform.rotation) gameobject;         activego.add (go);         if ( activego[0].transform.position.y < -2f)         {             deleteactivego ();         }     }     void deleteactivego ()     {         destroy (activego [0]);         activego.removeat (0);     } } 

i update. problem was trying 2 thing in 1 script...short story: solve problem created empty object in scene, divided script in 2 separated scripts, 1 makes spawned object move faster , 1 spawning object, put script moves object on object spawned , other script on empty gameobject spawn "stars" , worked charm. thank answers!

here final scripts:

spawning script:

using system.collections; using system.collections.generic; using unityengine;  public class spawnstars : monobehaviour {      public gameobject[] starstospawn;      private list<gameobject> spawnedstars;      private float randomx;      void start ()      {         spawnedstars = new list<gameobject> ();         invokerepeating ("spawnstar", 0f, 3f);     }     void spawnstar ()      {         randomx = random.range (-3, 3);         gameobject go;         go = instantiate ( starstospawn[0], new vector3 (randomx, 5f, 0f), transform.rotation);          spawnedstars.add (go);          if (spawnedstars.count > 2 )          {             destroy (spawnedstars [0]);             spawnedstars.removeat (0);         }     } } 

moving script:

using system.collections; using system.collections.generic; using unityengine;  public class movestar : monobehaviour {      public float acceleration = -5f;      void fixedupdate ()      {         getcomponent<rigidbody>().addforce( new vector3( 0f, acceleration,0f));     } } 

your code invoke ("instantiatestar", 2f); call once.

you can change invokerepeating("instantiatestar",2f,2f );

the code getcomponent<rigidbody>().addforce( new vector3( 0f, addedspeed, 0f)); seem should attach generate gameobject.

also note delete condition.

good luck.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -