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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

NullReferenceObject

Discussion in 'Scripting' started by orbitagar, Apr 16, 2018.

  1. orbitagar

    orbitagar

    Joined:
    Apr 16, 2018
    Posts:
    12
    i was following a guy's tutorial named brackeys and the script is supposed to move the character with a click but it gives me an error script for the AI:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. [RequireComponent(typeof(NavMeshAgent))]
    7. public class PlayerMotor : MonoBehaviour {
    8.  
    9.     NavMeshAgent agent;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         agent = GetComponent<NavMeshAgent>();
    14.     }
    15.  
    16.     public void MoveToPoint (Vector3 point)
    17.     {
    18.         agent.SetDestination(point);
    19.     }
    20. }
    And script for the movement:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(PlayerMotor))]
    6. public class PlayerController : MonoBehaviour {
    7.  
    8.     public LayerMask movementMask;
    9.  
    10.     Camera cam;
    11.     PlayerMotor motor;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.         cam = Camera.main;
    16.         motor = GetComponent<PlayerMotor>();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update () {
    21.         if (Input.GetMouseButtonDown(0))
    22.         {
    23.             Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    24.             RaycastHit hit;
    25.  
    26.             if (Physics.Raycast(ray, out hit, 100, movementMask))
    27.             {
    28.                 motor.MoveToPoint(hit.point);
    29.  
    30.                 // Stop focusing any objects
    31.             }
    32.         }
    33.     }
    34. }
    Error:
    NullReferenceException: Object reference not set to an instance of an object
    PlayerController.Update () (at Assets/PlayerController.cs:28)
     
    Last edited: Apr 16, 2018
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,383
    Use code tags and post the error verbatim.
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  4. orbitagar

    orbitagar

    Joined:
    Apr 16, 2018
    Posts:
    12
  5. orbitagar

    orbitagar

    Joined:
    Apr 16, 2018
    Posts:
    12
    I fixed it thx for telling <3
     
  6. orbitagar

    orbitagar

    Joined:
    Apr 16, 2018
    Posts:
    12
    I still need help though but now ive done the post right
     
  7. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,383
    Use Debug.Log() before the line throwing errors to see what is null on that line.
     
  8. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,383
    Use Debug.Log() before the line throw to see what is null on that line. Most likely it's the motor.
     
  9. orbitagar

    orbitagar

    Joined:
    Apr 16, 2018
    Posts:
    12
    I did use the debuglog and it said that it was ground (which was the layer called for the ai) so idk what to do
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    That is pretty strange, since the script requires the Motor.
    Does 'motor' look as though it's added & assigned?
     
  11. orbitagar

    orbitagar

    Joined:
    Apr 16, 2018
    Posts:
    12
    what do you mean by assigned? since the motor is just a script for the ai to work and find the best path via the navmeshagent (im a new developer and wanna be good so im not the best in unity)
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    All I was asking was if the PlayerMotor script was also on the game object, which I imagine it should be.. and then I'm not sure how it could be null.
    Well, do you have any other errors? For instance, if Camera.main was not found, the code in Start() to get the motor would never be run.
    Camera.main finds the camera if it's tagged with MainCamera. Is your camera tagged with that?
     
  13. orbitagar

    orbitagar

    Joined:
    Apr 16, 2018
    Posts:
    12
    i dont think so where can i tag it? =)
     
  14. orbitagar

    orbitagar

    Joined:
    Apr 16, 2018
    Posts:
    12
    I found out where to check and its tagged as main camera
     
  15. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ya, okay, so it's not that. And I think I may have been wrong, too, as it may have just returned null if it didn't find the camera.

    Anyways, I'm a little stumped by your problem.
    if you modify Start() to say this:
    Code (csharp):
    1.  
    2. void Start () {
    3.         cam = Camera.main;
    4.         motor = GetComponent<PlayerMotor>();
    5.         print("Is motor null? " + (motor == null).ToString());
    6.     }
    What does it say in the console? lol
     
  16. orbitagar

    orbitagar

    Joined:
    Apr 16, 2018
    Posts:
    12
    Is motor null? True
    UnityEngine.MonoBehaviour:print(Object)
    PlayerController:Start() (at Assets/PlayerController.cs:18)
     
  17. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Yep, so somehow it's not added, though it's a required component.
    I'm not really sure why that is, but does the filename of the script PlayerMotor match the class name properly?

    Just a wild guess as to what might be wrong, especially if you have no other errors.
     
  18. orbitagar

    orbitagar

    Joined:
    Apr 16, 2018
    Posts:
    12
    Its spelled exactly like in the script IDK what is wrong
     
  19. orbitagar

    orbitagar

    Joined:
    Apr 16, 2018
    Posts:
    12
    Found out that the script was not on the player base....... BRUH but thx
     
  20. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I see. Glad you got it solved. :)