I am making a game, and I have this script which is attached to a fruit gameobject. The fruit is attached to a game manager script. I have 3 fruits and each have their own fruitclickhandler
script. I also have 3 game manager scripts (0,1,2).
My problem is that I am trying to find the correct game manager, but it is null. I am confused because it is working for my fruitclickhandler
script, but not my fruitclickhandler1
script. Here is my code below.(Also, I have confirmed that both OnMouseDown
methods are being called).
public class FruitClickHandler : MonoBehaviour
{
private void OnMouseDown()
{
GameManager gameManager = GameObject.FindWithTag("Game Manager").GetComponent<GameManager>();
if (gameManager != null)
{
gameManager.UpdateScore();
}
else
{
Debug.LogError("GameManager not found in the scene.");
}
Destroy(gameObject);
}
private void OnTriggerEnter(Collider other)
{
Destroy(gameObject);
}
}
public class FruitClickHandler1 : MonoBehaviour
{
private void OnMouseDown()
{
GameManager gameManager1 = GameObject.FindWithTag("Game Manager 1").GetComponent<GameManager>();
if (gameManager1 != null)
{
gameManager1.UpdateScore();
}
else
{
Debug.LogError("GameManager1 not found in the scene.");
}
Destroy(gameObject);
}
private void OnTriggerEnter(Collider other)
{
Destroy(gameObject);
}
}
I would appreciate any suggestions, and if you need to see my other scripts or a screenshot I would be happy to attach one. Thanks.