- Home /
How to look for a game object and a change some components on that object?
Lets say I have 3 images; image1, image2 and image3. and 3 boxes, box1, box2, and box3. If I click image1 first it will go to box1, then image2 to box2 and image3 to box3. On another run, if I click image2 first, it will go to box1, then next image to box2 and next image to box3. So now, the problem or what I want to really happen now is if I click box1 or any other box, the image in box1 should return to the image where it originally belong. say, box1 has image2 so when I click box1, image2 will return to its original state.
PS: I am a beginner in UNITY so please bare with me. I appreciate some answer. and I apologize if my question has already been asked just provide me the link to the answer. Thank you so much.
Answer by KaspianR · May 18, 2018 at 10:33 AM
I can't really help you with creating that mechanic since it's to much to do inside of just scripts but I can help you with finding the object: GameObject.Find("The name of the object")
will give you the object with that name. And you can use GameObject.Find("The name of the object").GetComponent<The component you wan't to use>()
to find the component you need. This is one way to do it but if you want an easier way you can just make a public variable of type GameObject (Transform would work as well). And then just drag your Image/Box to the slot that appeared in the inspector! Is this what you wanted?
Thank you @$$anonymous$$aspianR , I stored the gameObject.name to a string, say name1, and use name1 in GameObject.Find(name1).GetComponent< my component to use>() but it returned a NullReferenceException: Object reference not set to an instance of an object ClickControl.On$$anonymous$$ouseDown ().
Did you replace "my component to use" with the component you wanted to find? If you want to find a specific script you can just write the name of that script and if you need the position you can use GameObject.Find(name1).transform.position
If this still doesn't work please provide a bit more information about what component you want to change and how!
Never$$anonymous$$d, it worked! there was just some error in my code. Thank you so much!