- Home /
how to select and deselct an instantiated objects.
Hello
I am making a script. When i press the button a gameobject is instantiated. When i press button again then the another gameobject(clone) is instantiated. What i want to do is make a select and deselect between gameobjects. Lets say that i instantiate 10 objects when game is running. Then i select one gameobject and it turns red. Now i want that if i select another gameobject the the first gameobject will deselect itself and new chosed gameobject will be red.
Basically i dont want to be able to choose more than 1 object. Right now my have made is so that i can select and deselect all the gameobjects when i klick on them. But i just want to able to select one gameobject. And after i select another the first one will get deselected by default.
If this were static objects i just could do a bunch of static boolean fields and make a simple (if check) between them. But all my objects are can be created dynamically at runtime. So i dont know how to make these object interact between each other. All clones are having the same script so how can they know when something other besides them is selected.
Thanks.
Answer by Uriel_96 · Dec 31, 2010 at 08:22 PM
you may see this answer: http://answers.unity3d.com/questions/28251/how-to-perform-a-mouse-click-on-game-object/28252#28252 and you only change where it says the tag to a different color. Like this:
renderer.material.color = Color.red;
Answer by Jaap Kreijkamp · Jan 01, 2011 at 02:22 AM
If they share the same script you can share state using static variables. In Javascript static var someGlobal
and in C# public static TypeName someGlobal
Answer by DryTear · Feb 04, 2013 at 03:13 AM
Attached to a Cube with a collider
var customColor : Color;
OnMouseEnter(){
renderer.material.color = customColor;
}
OnMouseExit(){
renderer.material.color = Color.white;
}
Your answer
Follow this Question
Related Questions
How do I revert prefab instance properties to their defaults? 0 Answers
How can i transfer an object from scene1 and instantiate it in scene2? 2 Answers
How can I Instantiate an Object from a Randomly Selected Index in an Array of Game Objects? 1 Answer