Help C# Get Component does'nt Work

Hey I Have a Rpg game and i need help with this script
It’s supposed to Make it so when the main character goes into a trigger the bad guy moves towards it

the moving works but the trigger part errors when i enter play mode

public class LookMove : MonoBehaviour
{
    Triggered trig;
    public GameObject Trig;
    public int Hello = 0;
    public Transform target2;
    public Transform target;
    NavMeshAgent agent;
    void Start ()
    {
        trig = Trig.GetComponent<Triggered>();
        agent = GetComponent<NavMeshAgent>();
    }
    void Update ()
    {
        trig.Cool = Hello;
        if(Hello == 1)
        {
            agent.SetDestination(target.position);
        }
    }
}

It has an error message that says something in line 16 doesn’t exist

public class Triggered : MonoBehaviour
{
    public int Cool = 0;
    public GameObject Trig;

    void OnTriggerEnter (Collider other)
    {
        if(gameObject.name == "OrcTrigger")
        {
            Cool = 1;
        }
    }

}

here’s the other script

Any help would be appreciated. :slight_smile:

And that specific GameObject has both those component on itself?

Yes the main character has both scripts on it
The exact error message is here

NullReferenceException: Object reference not set to an instance of an object
LookMove.Update () (at Assets/LookMove.cs:19)

Try to add the if statement after your GetComponent.

agent = GetComponent<NavMeshAgent>();
if (agent == null) {
    Debug.Log ("No agent found", gameObject);
}

If you select the message in the console, it will highlight the game object in the hierarchy that doesn’t have a NavMeshAgent.

It didn’t Do anything if that tells you anything

[QUOTE=“unity2727, post: 1720113, member: 576816”

public class LookMove : MonoBehaviour
trig.Cool = Hello;
if(Hello == 1)

[/QUOTE]
So you are setting your Cool variable inside your Triggered class to Hello(which will always be 0), then check to see if Hello is equal to 1? That code will never run since you never change the value of hello.

About your error messgae did you set your traget variable to something?

I have checked line 19, so there is either an issue with agent, which doesn’t seem to be the case, or there may be an issue with target. Did you check that target is not null?