How can i reach other object in script of a object?
I want to compare a sphere color with a cube color. But i don't know how can i reach the sphere in script of the cube. May you help me?
Yes i'm new to Unity.
Comment
Best Answer
Answer by akiva42 · Jun 24, 2016 at 06:30 AM
One way to do this is with:
GameObject.Find("NameOfSphere").GetComponent<Renderer>().material.color
Basically the first part will search all the active GameObjects and find the one with the name you provide. The second part will get that objects Renderer, the material used by that Renderer and then the color of that material.
It's not recommended that you use Find in the update loop (every frame) because it will slow things down. So you may want to save the sphere once like this in Start:
GameObject externalSphere = GameObject.Find("NameOfSphere");
And then you can refer to the color like this:
externalSphere.GetComponent<Renderer>().material.color
Your answer
![](https://koobas.hobune.stream/wayback/20220612083548im_/https://answers.unity.com/themes/thub/images/avi.jpg)