- Home /
Storing the status of a GameObject.
Hello there,
Im new to Unity, and i got a small problem.
Imagine i have a square, on which another gameobject should become visible after i click on that gameobject. It should however only happen once, and on the second click it should do nothing.
I can create the object on a click, but how can i store the fact that the gameobject has been clicked already, so it wont create a new gameobject on every click. What i would like is for example a variable named "inUse" attached to every GameObject, which can have the value true or false. Ofcourse, when a new gameobject gets created it should be set to false.
I think, i need to add a new component or something to the gameobject that keeps track of this, but i cant seem to do that ;)
I hope someone can help.
Thanks in advance.
Answer by StephanK · Apr 15, 2011 at 12:49 PM
Why don't you just add the variable to the script that does the instantiating?
Answer by Joshua · Apr 15, 2011 at 01:26 PM
as Spree said, add the value to the script that does the instantiating. This script will get added to each of the gameobjects. At the top add:
var clicked = false;
and after the if function that checks if it's clicked add
clicked = true;.
the if function I mentioned should look something like this:
if(mouseclick) {//do stuff)
make it (mouseclick && !clicked) {//dostuff}
Thanks both for the answers. I did that, but now im running into more problems ;) In short, i got it working, however, now i want to check from the script, if any of the other objects are also clicked.
my code:
public class click : $$anonymous$$onoBehaviour { public bool isclicked = false; public int clickedby = 0;
void checkWinner() {
print(Cube5.clickedby);
print(clickedby);
The first print does not work, the second prints the value in clickedby, of the current object. How can i read te value of the other object ?
You should open a new question for this, so others may find the answer to this question.