- Home /
Sharing components amongst different gameobjects
Easy question, but pretty difficult to figure out the answer despite modest unity knowledge.
I have a scene with a few 'master gameobjects', each with the same script that holds game state variables, but with different values.
master1GO <= variable 1 = "a", variable 2 = "b", variable 3 = "c"
master2GO <= variable 1 = "d", variable 2 = "e", variable 3 = "f"
master3GO <= variable 1 = "g", variable 2 = "h", variable 3 = "i"
Then spontaneously, these gameobjects will get duplicated by the scene, up to several hundred times, with their transforms roughly randomized for scattering the things clones around.
The problem is, each instance will hold its very own game state, with its own script instance, while my problem is that I need a -single- script behind all the 'clone' instances of the same script, since I'd like to change only the "original" gamestate in master1GO, for example, and thus alter the same values in all "master1GOclones", and this is why I can't however use a static reference, since I have a random number of masterGOs with the same script of course.
Is there a way to achieve such result? I basically need to instantiate a clone of an existing gameobject, and then to substitute the cloned object script of one specific class with the original script's reference of the same class.
thanks
Answer by Waz · Aug 17, 2011 at 01:09 PM
Put the variables in a class, and reuse that:
// MyScript.js
class Variables { var v1 : int; var v2 : int; ...}
var variables = new Variables();
// In copying script
var clone = Instantiate(master1);
var script = clone.GetComponent.<MyScript>();
var original = master1.GetComponent.<MyScript>();
script.variables = original.variables;
Changing variables.v1
in the master will now be changing it in all copies.
$$anonymous$$akes sense - you're passing by reference rather than value.
One issue that I wonder about is whether this adds significant overhead in terms of time. He stated that he 'spontaneously' adds several hundred of these, which I take to mean in a frame or several frames. I've found that just instantiating hundreds of game objects can take up to several seconds, but now we're adding getComponent calls into the mix. How quickly does Unity look up components? The actual variable assignment, if it's simple types, would be fairly non-noticeable, but roamcel, are you going to include any arrays or other large data sets in your script?
thanks for the incredibly straightforward solution.
Regarding data size, NOAA, I'll be actually 'moving' two lists and an array of 15some values each, but I'm not worried about 'spawn time' since spawning is not time critical, fortunately, lol. Items will be actually 'faded' into scene.
@NOAA_Julie has a good point, though the Instantiate will far outweigh the GetComponent calls. If it's a problem, I'd advise spreading out the spawning over a few frames. i.e. ins$$anonymous$$d of 200 at once, do 10 per frame for 20 frames (about 0.3 seconds, so it'll be "instant" to the user).
Your answer
Follow this Question
Related Questions
Does Destroy() destroy all components? 2 Answers
Clearing a LineRenderer Whiteboard, so that you can start drawing again 1 Answer
Execute Code When Component Added 6 Answers
GameObject stutters when moving if refresh rate is not set to 60hz. 1 Answer
Exposing Components to different scripts,Proper way to expose components to different scipts 0 Answers