The below script is attached to 25 game objects, after 1 is destroyed count gets set to 24, then stays at 24 despite others being destroyed.
public class shootable : MonoBehaviour {
public Text countText;
private int count = 25;
void Start()
{
Debug.Log("Start" + count);
//count = 25;
setCountText();
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Shot")
{
Debug.Log("b4 " + count);
Destroy(other.gameObject);
Destroy(gameObject);
count--;
Debug.Log("after " + count);
setCountText();
}
if (count == 0)
{
Application.LoadLevel("Level2");
}
}
void setCountText()
{
countText.text = ("Enemies Remaining: " + count.ToString());
}
}
Comment
Best Answer
Answer by PizzaPie · Nov 07, 2016 at 02:23 PM
Well if it is attached to 25 objects you have 25 different counts. You got to have a single class which contains the count seperated from all the objects. Then from that script you got, access that class and change the value of the count. Something like
public class GlobalCounter
{
public static int count;
}
and from your Script remove count and replace em with GlobalCounter.count++ or whatever you need.
Your answer
Follow this Question
Related Questions
RayCast 2d change start position ? 0 Answers
Calculate BoxCollider2D based on the actual player sprite 2 Answers
I need help with AI,Force not working 0 Answers
How do I make a ball bounce in a circle? 0 Answers