Calling a function from colliding with an Object

Hello everybody,

My goal is to:

  1. have a “player” enter a collider
  2. the collider has a script on it, that once the player enters it, it’s calling a function from a Patrol script on the “avatar”.
  3. the “avatar” now needs to go to a new destination of the “player” (deviate from the patrol points).
  4. the “avatar” needs to stand by the user.

This is the fun. on the collider:

 void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
            print("a player in the colider"); 	
		{
			foreach (GameObject avatar in avatars)  
			{
                if (Vector3.Distance(other.gameObject.transform.position, avatar.transform.position) <= minDistance)
                {
                    PS = avatar.GetComponent<Patrol>();
                    if (PS != null) 
                    {

                        PS.GoToSpecificPoint(other.gameObject.transform);

}

here’s a code in the “avatar”:

 public void GoToSpecificPoint(Transform targetLocation)
    {
        agent.destination = targetLocation.position;
         if (Vector3.Distance (agent.destination, targetLocation.position)<= 0.1f)
        {
            agent.Stop();
            
        }

For some reason it’s not working, and on one of the projects it gives me an error:
NullReferenceException: Object reference not set to an instance of an object
Patrol.GoToSpecificPoint (UnityEngine.Transform targetLocation) (at Assets/Scripts/Patrol.cs:84)
ShelfEvent.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/ShelfEvent.cs:38)

Which seems to be the problem,
that the agent.destination is set to “targetLocation” but it seems to be not enough.

Thanks to any possible ideas.

Thanx Nyonge,

What I actually did to solve this is I put the calling function on my Player, so when the player is colliding with the “collider”, the function is called. I reversed the logic. like with Muhammad and the mountain :slight_smile:
Anyway I wish to have a good solution for my first approach as well… I’ll try what you said.
But for now I guess this is solved.

NullReferenceException means one of the variables you’re attempting to access is null. The two variables you first use in GoToSpecificPoint are “agent” and “targetLocation”. targetLocation is probably not null, because you’re calling it from an OnTriggerEnter, so it’s most likely “agent” that’s null.

Putting a check for

if (agent != null) {

will prevent the error. Make sure agent is properly being assigned on the script before using it. You can also throw in

if (agent == null) {
Debug.LogError("Error, agent is null, can't GoToSpecificPoint", gameObject);
} else {
//move to destination
}

to help debug when the issue happens. Good luck!

You could make an “if statement” that will call a function by functionName() when the if statement it true.

@username
Best way of Doing this is to use Events.You Don’t need to take references or composition in your scripts.

public class UnityFunction
{
    public UnityEvent Collidinginfo;

    private void Start()
    {
        if (Collidinginfo != null)
            Collidinginfo.AddListener(delegate
            {
                //do what you want
            });
    }

    private void OnTriggerEnter(Collider col)
    {
        //then inform to All scripts or function that collide with points
        if (Collidinginfo != null) Collidinginfo.Invoke();
    }
}