- Home /
How to make single score affected by multiple collision objects?
I am trying to make a simple pachinko game. What should happen is, a ball spawns, the count decreases by 1, and if the ball lands in a basket, the count goes back up. I am able to make the score decrease and increase. But these scores seem to be individual from each other.
For example: I start at 100 balls. I spawn one ball and now have 99 remaining. The ball lands in the basket and I'm back to 100. However, the next time I spawn a ball I now have 98 instead of 99. It's as if the decreasing and increasing of the score are out of sync.
How would I go about fixing this? Here's my code: public class GameManager : MonoBehaviour { public GameObject ballPrefab;
public Text CounterText;
public static int Count = 100;
// Start is called before the first frame update
void Start()
{
Count = 100;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("space"))
{
Instantiate(ballPrefab);
}
}
public void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "LoseBall")
{
GameManager.Count -= 1;
}
if (other.gameObject.tag == "Gain1Ball")
{
GameManager.Count += 1;
}
}
}
Comment
Your answer