I’m currenlty working on a project and I’ve gotten stuck on a really simple thing, I’m trying to make it so that if the player picks up a certain item the player gets healed
(Player script)
public void Heal (int _healamount)
public void OnTriggerEnter (Collider info)
{
if (info.tag == “Player”)
{
Player playerheal = GetComponent();
playerheal.Heal(10);
Destroy(gameObject);
}
}
}
but now when I move the player into the object it comes up with an error:
NullReferenceException: Object reference not set to an instance of an object
HealthUp.OnTriggerEnter (UnityEngine.Collider info) (at Assets/HealthUp.cs:14)
I’ve looked around but I’m not sure why, any ideas?
Referencing variables, fields, methods (anything non-static) in other script instances:
REMEMBER: it isn’t always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.
Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.
That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.
Well does your script have a variable/member called info? Probably not. Because the example is contextual to OP’s code.
Rather than copy code off random posts (which is a terrible idea), and necro an old post, it’d be wiser to make your own thread detailing your problem.