Changing a variable across multiple instances of a GameObject

Updated on January 13, 2018 in [A] Other Answers
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on January 11, 2018

I have several game objects with the same movement script. The movement script consist of an OnTrigger function and a update function that works together to increase the speed of the game object when it passes a collider with trigger enable. The problem is the speed only increase for the object that passes the collider, I would like to increase the speed simultaneously of all the game object with the movement script attach. I’m new to unity, any help will be much appreciated.

  • Liked by
Reply
0 on January 11, 2018

You probably want to make the speed variable static to the class instead. (probably depends on the use case)

  • Liked by
Reply
Cancel
0 on January 13, 2018

Instead of making static variables, loop through all the objects that you want to modify the speed variable, get the script on that object that holds the speed variable and adjust the value.

Edit; some psuedo code

onTriggerEnter {
  start forloop object type {
    get Script.speedvar = new speedvalue
  }
}

  • Liked by
Reply
Cancel