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. Dismiss Notice

Clones of Prefabs problem

Discussion in 'Scripting' started by verderveremem, Jul 26, 2014.

  1. verderveremem

    verderveremem

    Joined:
    May 11, 2014
    Posts:
    52
    Hello everyone, In some specific time periods, I want to speed up the steam of my prefabs which goes from left side of the screen to the righ side. And ı’m applying a force with the “Rigidbody.addforce” But ı want to apply this speeding up both to the prefabs which has already been produced and to the new ones which will be produced. Otherwise its being a syncronisation problem in the screen. Do you guys know how can ı do this?
     
  2. domkia

    domkia

    Joined:
    Aug 19, 2012
    Posts:
    99
    Why not to store current force and then asign it on instantiating.
     
  3. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    You could use a static speed variable in their script, then just increase the value and all gameObjects using the script will be able to use the value in their Update() methods.
     
  4. verderveremem

    verderveremem

    Joined:
    May 11, 2014
    Posts:
    52
    how can i change speed of prefabs previously created
     
  5. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    If you store a list or arraylist of all active objects then you can just loop through them adjusting their speed.
     
    verderveremem likes this.
  6. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Yep. You could likely use a listener for changes in speed as well. Not sure which is faster.
     
    verderveremem likes this.
  7. verderveremem

    verderveremem

    Joined:
    May 11, 2014
    Posts:
    52
    .Does prefabs automaticlly removed from arraylist when destroyed. Or can i scripting manuelly for this?

    and is there anyway to reach all clones which were already created without using arraylist?
     
  8. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    OK once you have instantiated a prefab it's a gameObject. You can use a script on the gameObject that uses the OnDestory() method to remove it from the arraylist.

    http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnDestroy.html

    If the cloned gameObjects all have the same script on them then you can use the static speed variable I mentioned before.
     
    verderveremem likes this.
  9. verderveremem

    verderveremem

    Joined:
    May 11, 2014
    Posts:
    52
    thank you too much for your answers, i understood which way i must follow. But i really hate to use arraylist. So is there anyway to reach all clones which were already created without using arraylist?
     
  10. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    You can do a GameObject.Find or have them subscribe to an event listener thing.
     
    verderveremem likes this.
  11. verderveremem

    verderveremem

    Joined:
    May 11, 2014
    Posts:
    52
    i have no idea how event listenig is working? and i didnt find this from scripting reference :/
     
  12. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    GameObject.Find (probably find by tag?) is likely the easiest.
     
    verderveremem likes this.