- Home /
How to count numbers on onmousedown?
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");
}
}
Answer by monogon · May 01, 2014 at 11:21 AM
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);
}
but i have, suppose 20 petals and on each petal this script is applied if one deactivate other are still active .. :/ ...i applied such a script in coins collection .. when you collide with coin it add 1 in the score and when you collide with second coin it add 1 again .. coin+=1; ... and coins do get deactivate/destroy.. but whats the matter with onmousedown() function :( ... yes last time i did in the update function for coins... but here when i do in the update function it deactivate all of the petals once rather then it should deactivate only one petal on one click...
function Update(){
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
petalStart=petalStart+1;
print("petals = "+petalStart);
if(petalStart%2==0)
{
Debug.Log("its even");
}
else{
Debug.Log("its odd");
}
}
}
I edited my answer above. Just replace your code with the provided one. You got me wrong last time. You weren't supposed to completely remove your line where you deactivate the gameObject. It was just at the wrong position. Furthermore you were using petalStart as a dynamic variable where you needed a static one. $$anonymous$$ark this question as solved please if the code works (it works ;) ).
Your the $$anonymous$$an ... $$anonymous$$uaaaaaaa :') .. Thanks ..
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to apply OnMouseDown on sprites according to z position... 3 Answers
RayCasting from camera to player 1 Answer
Raycast Help 1 Answer
Move Towards Target Color 2 Answers