Select Objects ingame and show information
I want to make a RTS and there i need to select single units, groups of units etc, but I kinda have a thinking barrier rn
I already made a script to "mark" (it just tints the object) and prints the name of the clicked object to the console.
So I decided to make a new script "Selectable" that every unit will get, here it is:
public bool selected;
void OnMouseDown() {
if (!selected) {
selected = true;
} else {
selected = false;
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (selected) {
gameObject.GetComponent<Renderer>().material.color = Color.red;
print (gameObject.name + " has been selected");
} else {
gameObject.GetComponent<Renderer>().material.color = Color.white;
}
}
Problems for me are: A normal left click should always deselect all other objects first (and I have no idea how to get a list of all gameObjects that have the script)
And the "print (gameObject.name + " has been selected");" only happens once no matter how often i click the object.
I solely write in C# and will use 3D graphics. Using Unity 5.6 free version (community or what it is called) Examples of scripts would be much appreciated.
Your answer
![](https://koobas.hobune.stream/wayback/20220612121410im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Enabling and Disabling a Canvas 1 Answer
Disable mouse click on gameObject 1 Answer
Activate when mouse klick object 1 Answer
Move object like in scene mode 0 Answers
How to change a game objects tag after it has been randomly selected? 0 Answers