Hi… i am stuck at one last point of my game … in my game i have flower which is made with petals(sprites) and petals are on one another to form a flower in z direction. each petal have 2d collider and my below script. what i am trying to do when user click on petal, petal get disappear and add 1 in the petal counter. when counter is even display its even, and when counter is odd number display its odd. now whats happening it just count once after that counter doesn’t count … it remains on 1 … and show its odd on every click… when i comment/remove the gameObject.active=false live it works well.on first click show odd on second show even… add 1 in every click…Every time i display different number of petals so we click on them untill petals get end… and at the end either print odd or even …
internal var petalStart:int;
function Start(){
Debug.Log(petalStart);
}
function OnMouseDown()
{
gameObject.active=false;
petalStart=petalStart+1;
print("petal = "+petalStart);
if(petalStart%2==0)
{
Debug.Log("its even");
}
else{
Debug.Log("its odd");
}
}
Like you say you deactivate the gameObject when the user clicks on it, so it cannot receive further events.
Also it seems that you need a static variable if you want this counter to count globally within the class.
private static var petalStart:int
With your implementation petalStart is unique for each instance, so it never gets greater than 1 because you deactivate the instance on click.
ps.: create a debug.log for this new static variable because you cannot see it in the inspector.
Edit:
There were different problems with your script. First of all you deactivated the gameObject too early, before the variable has been incremented. Secondly gameobject.active is deprecated, use gameObject.SetActive(value: bool) instead like you see in the code below. At last I changed your variable to a static member like I said earlier. (I’ve just tested this code, it works - just replace it with your code)
#pragma strict
private static var petalStart:int;
function Start(){
Debug.Log(petalStart);
}
function OnMouseDown()
{
petalStart++;
print("petal = "+petalStart);
if(petalStart%2==0)
{
Debug.Log("its even");
}
else{
Debug.Log("its odd");
}
gameObject.SetActive(false);
Destroy(gameObject);
}