I don't understand the mistake, I'm a newbie

Error:
NullReferenceException: Object reference not set to an instance of an object
VidaExtra.SumaDeVida (UnityEngine.Collider player) (at Assets/Scripts/VidaExtra.cs:35)
VidaExtra.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/VidaExtra.cs:26)

public class VidaExtra : MonoBehaviour
{
    public int CantidadDeVida;

    Health Vida;


    private void Awake()
    {

        Vida = GetComponent<Health>();

    }

    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            SumaDeVida(other);

        }

    }


    void SumaDeVida(Collider player)
    {
        CantidadDeVida += Vida.currenthealth;

       // Destroy(gameObject);
    }
}

Did you attach a “Health” component to the same GameObject this script is running on?

how?

By dragging the “Health” script onto the object in the inspector, the same way you attach any script.

necessary? because I’m calling him, I don’t know if it’s necessary to put it

In Awake, check to make sure that Vida is not null.

1 Like

Completely necessary. GetComponent expects the component (in this case a script) to be attached to the Game Object that it’s being called on. If a component isn’t there it will return null and then when you go to use it you will get the error messages you’re getting now.

2 Likes

I already solved the problem, thank you all