OnMouseDown
Hi! I am trying to create a 2D game. I have a tree made with three square sprites one on top of the other inside an empty GameObject, and I want to deactivate them one by one with the mouse. The problem with the code I wrote is that when i click the tree, the trhee of them deactivate. I tryied in two different ways and didn't work. And what I want is: One click, one deactivates, another click, and then the other one deactivates, and one last click and the whole tree deactivates. Have any Ideas? This is my first time posting something here so I don't know if this is the right place to do this questions or if I am doing it right. This is the code and thanks!:
public class Tree : MonoBehaviour {
public GameObject[] contadorTroncos; GameObject arbol;
// Use this for initialization
void Start ()
{
arbol = GameObject.Find("Arbol");
contadorTroncos = GameObject.FindGameObjectsWithTag("Tronco");
}
// Update is called once per frame
void Update ()
{
ColocarTroncos();
}
void ColocarTroncos()
{
for (int i= 0; i < contadorTroncos.Length; i++)
{
contadorTroncos[i].gameObject.transform.parent = arbol.transform;
}
}
private void OnMouseDown()
{
int i = 0;
if(i == 0)
{
contadorTroncos[0].gameObject.SetActive(false);
i++;
}
if(i == 1)
{
contadorTroncos[1].gameObject.SetActive(false);
i++;
}
if(i == 2)
{
contadorTroncos[2].gameObject.SetActive(false);
}
/*
for (int i = 0; i < contadorTroncos.Length; i++)
{
switch (i)
{
case 0:
contadorTroncos[0].gameObject.SetActive(false);
break;
case 1:
contadorTroncos[1].gameObject.SetActive(false);
break;
case 2:
contadorTroncos[2].gameObject.SetActive(false);
break;
default:
break;
}
}
*/
}
}
Your answer
Follow this Question
Related Questions
Defining Rectangles on the fly 1 Answer
How would I create a script for charging a energy meter while the character is moving ? 0 Answers
boomerang System(3d top view ) 0 Answers
How do I maintain the same X and Y coordinates when changing scenes? 1 Answer
I need to bend a stick and am lost 1 Answer