Effecting other game objects remotely
I'm new to scripting and whilst I've muddled through using this forum, google and Youtube so far, I've hit a bit of a brick wall. I'm trying to figure out how to get a script on one game object to change a property on another game object. For example, I have MasterObject with the script attached to it. I then also have OtherObject01, OtherObject02 and OtherObject03. When an integer variable in the script on MasterObject reads 1 (for example), I want it to tell the renderer in OtherObject02 and OtherObject03 to turn off - ie, set it to false but I cannot figure out how to specify those objects.
Initially I added a script to the OtherObjects that listened for the variable value and turned themselves off if the correct condition was met, but if I have dozens or hundreds of these OtherObjects, that means dozens or hundreds of three-line scripts which seems horribly inefficient. Equally, I have encountered the GetComponent command but as yet have been unable to get it to refer to a specific game object.
I did manage to get this to work:
public var OtherObject01: GameObject;
function Update() {
if(Setup.department == 1) {
OtherObject01.GetComponent.<Renderer>().enabled = true;
}
else {
OtherObject01.GetComponent.<Renderer>().enabled = false;
}
But this sets up public variables and I have to manually assign the game objects to those variables in the inspector. It must be possible to assign those game objects within the script or just name them directly. I just can't figure out how. Any help would be much appreciated.
Answer by Wayne_Peters · Feb 04, 2017 at 02:36 PM
After much research and help from an old colleague via social media, the answer seems to be to use Tags and GameObject.FindWithTag. I've tested it and it seems to work. It's pretty cack-handed having to set it up as a variable and not being able to address the game object directly but it works and appears to be the least inefficient way to go.
It turns out that this won't work as I can only have nine tags and I need hundreds. The only way I can see of this working is to give each object it's own script that listens for variables. That just seems horribly inefficient but as I'm a newbie to scripting, I can't see any other way of doing it.
Well GameObject.Find works great and I've switched to deactivating objects rather than shutting off their renderer (because you can't do that with Canvas objects apparently!).