- Home /
Changing a variable across multiple instances of a GameObject
Hello Everyone. I need a little bit of help defining the best way to go about a problem I have. Here is the situation..
I have many instances of a prefab that have a fall speed variable. I have another "power up" gameobject that, when destroyed, slows the fall speed of all instances in the scene. I am having issues figuring out the best way to go about this since it will be a mobile app and performance is an issue.
Idea 1: using findObjectsWithTag
I thought of using findObjectsWithTag to gather all objects with types of both "orb" and "powerup" in the scene, and then iterate through that array changing the fall speed variable of each to half of what it was. The issue I have with this is there are 2 different tags I need to gather, and I dont think I can do something like findObjectsWithTag(orb && powerup)
Idea 2: Use a static or global variable that all objects in the scene access - I could also use a static or global variable in the game manager script that holds the fall speed of all of the objects in a scene, and then have the objects check with the game manager for what their fall speed should be. This would be too resource heavy because it requires all of the objects in the scene to constantly check for the speed they should be falling at.
I guess I am looking for a way to change one variable and it effects the fall speed of every object in the scene for a set amount of time before it returns to normal. I would be very grateful if someone can offer their advice.
Thanks!
Answer by haim96 · Mar 13, 2014 at 08:33 PM
you could use static variable but i think that using Events can help you with this. this way, the object doesn't need to check the variable all the time but get notified when it changed. more about events here: http://unity3d.com/learn/tutorials/modules/intermediate/scripting/events
for sure i can tell you NOT to use Tags for this. it slow.
Wow this is great information! I am going to watch all of those videos in that intermediate section to become more comfortable with these concepts. I have a feeling I am going to have to restructure my design, but it will be worth the effort.
In that video, the man stated that you basically create a method that other methods can subscribe to in order to be called when the event occurs. He also stated that for every subscription, there should also be an unsubscribe. What about scenes with many instances of an object? Does each separate instance have to subscribe and unsubscribe from the event handler? Does this mean that an event handler is handling multiple instances of a Function()? If I unsubscribe a Function() from the event handler, does that effect all other instances of that object?
Thanks for the help. This is really going to strengthen my program$$anonymous$$g skills. Opens up many more possibilities and makes many processes simpler.
Your answer
Follow this Question
Related Questions
Script that stores gameobject hit by raycast keeps getting a NullReferenceException error. 2 Answers
Assigning current color to a variable for fade out (C#) 0 Answers
Communication between objects and other scripts, variables and properties 1 Answer
Game Object referencing from another script 0 Answers
Public variable in script different for every game object. 1 Answer