jeffrey guthery (gojeff321)

4 answers · asked · Lesson: Scripting Enemy Detection · Course: Fundamentals of Pathfinding

Question on Detection Script

The Detection Script in the downloaded "Detection.unity" Tutorial Scene is generating the Console Warning shown below. It can be assigned to and adjusted within the Enemy Players' Component window, but the Viewing Radius/Angle and color are not visible in the Screen window. The script appears to be identical to that shown in the video, but I may be missing something, or maybe recent updates necessitate minor script changes. If you have a fix please let me know. Thank you.

NullReferenceException: Object reference not set to an instance of an object
Detection.Start () (at Assets/Pathfinding2017/Exercise/_Start/Scripts/Detection.cs:22)

  • crew

    Whenever you see "Object reference" errors it's because it's missing something it's trying to access. In this case on line 22 as the error mentions, this refers to the "GameController". In the script it finds the game object that has the GameManager script by referencing a tag called "GameController". So if you placed the GameManager script on an object in your scene, make sure that object also has a tag of GameController assigned to it so the script can easily find it. 

    Another simpler way is to just delete the line:

       gameManager = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameManager> ();

        In the Detection script and manually assign the "gameManager" object. This would be the object that contains the "GameManager" script. That way you don't need to search for it, it'll already be assigned to the script. If you have any other questions let me know, thanks. 

  • Got it - Thank You.

  • crew
    I also updated the "complete" scene in the project files. I noticed the completed scene had a few components turned off or were missing some things that were assigned. This completed scene should have everything you need for this to work properly. Take a look at the "GameOverHUD" in the ExerciseCompleted scene and you'll see it has the GameManager script on it with the tag of GameController. 
  • Fantastic. Thank you very much - - - -