I have this code and I want to trigger the function Sumar() in another script called Score, there is an empty GameObject in my scene which has attached the Score script, this empty GameObject only display text, while the ItemScript which has this function is attached to a Sprite.
void OnCollisionEnter2D(Collision2D collision)
{
// Colision con el jugador
PlayerScript player = collision.gameObject.GetComponent<PlayerScript>();
Score sumaPuntaje = this.GetComponent<Score>();
sumaPuntaje.Sumar();
if (player != null)
{
Destroy(gameObject);
}
}
}
But I keep getting this error:
NullReferenceException: Object reference not set to an instance of an object
I’ve tried with other instructions like this one:
Score sumaPuntaje = gameObject.GetComponent<Score>();
Score sumaPuntaje = GetComponent<Score>();
But I keep getting the error message, what can I do in orther to solve this?