Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Why dosent this work?!

Discussion in 'Scripting' started by beniboy2003, Oct 2, 2016.

  1. beniboy2003

    beniboy2003

    Joined:
    May 15, 2016
    Posts:
    19
    im having problems with this code for an enemy AI to follow me. this is not meant to attack me yet and i do have a sphere collider on the enemy(this is also set as trigger) ive placed my script on the enemy too.

    this is the code:

    using UnityEngine;
    using System.Collections;

    public class BasicAI : MonoBehaviour {

    private NavMeshAgent agent;
    private Transform PlayerTransform;

    private void start()
    {
    agent = GetComponent<NavMeshAgent>();
    }

    private void OnTriggerEnter(Collider target)
    {
    if (target.tag == "Player")
    {
    PlayerTransform = target.transform;
    agent.SetDestination(PlayerTransform.position);
    }
    }
    }


    error:
    NullReferenceException: object reference not set to an instance of an object



    can anyone help me??!!
     
  2. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    Please use proper code tags when posting code.

    Function names are case sensitive. You have "private void start()" which should just be "void Start()". Please use the following.
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class BasicAI : MonoBehaviour
    4. {
    5.    private NavMeshAgent agent;
    6.    private Transform target;
    7.  
    8.    void Start()
    9.    {
    10.      this.agent = this.GetComponent<NavMeshAgent>();
    11.      if (this.agent == null)
    12.      {
    13.        throw new UnityException("Missing NavMeshAgent!");
    14.      }
    15.    }
    16.  
    17.    void OnTriggerEnter(Collider target)
    18.    {
    19.      if (target.CompareTag("Player"))
    20.      {
    21.        this.target = target.transform;
    22.        this.agent.SetDestination(this.target.position);
    23.      }
    24.    }
    25. }
     
    Ryiah and Kiwasi like this.
  3. beniboy2003

    beniboy2003

    Joined:
    May 15, 2016
    Posts:
    19
    thx for the help! it works yipee
     
  4. Kalladystine

    Kalladystine

    Joined:
    Jan 12, 2015
    Posts:
    227
    One tip to add:
    Use with caution, though (see later posts from jimroberts why)
    To avoid checking that the correct component (in this case NavMeshAgent) is also attached to the GameObject, you can require it - see here for the API documentation.
    That way if you add your BasicAI script to a GameObject, it will automatically add NavMeshAgent if one is not present.
     
    Last edited: Oct 3, 2016
    Ryiah likes this.
  5. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    This could be a really bad idea if you are expecting the values of your component to be set via the inspector. In my opinion it is better to throw an exception describing which component is missing so you can go configure it correctly. Using the RequireComponent attribute feels like you're just sweeping the problem under the rug.
     
    Kalladystine likes this.
  6. Kalladystine

    Kalladystine

    Joined:
    Jan 12, 2015
    Posts:
    227
    Depends on what you're used to I suppose. Your method is definitely safer.
    RequireComponent does come handy if you're not relying on inspector set values and are chaining components (since AddComponent will also add all RequiredComponents if used from code side). That way if I'm building objects from code and I need f.e. to change a rock into a unit, I can AddComponent<EnemyAI> and chain other needed components from there with default values, adjusting only what's needed.

    Either way, I agree that this can be abused and/or misused and I probably shouldn't have added it to a beginner level topic. I'll edit to add warning there.