Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

I keep getting NullReference Errors

Discussion in 'Scripting' started by iskippyrs, Nov 24, 2017.

  1. iskippyrs

    iskippyrs

    Joined:
    Nov 23, 2017
    Posts:
    6
    So I am new to Unity, I started following Brackeys guide on this one





    However at 11:15 when we were going to walk the character I tabbed back into Unity after finishing the script.

    (I’ve rewatched the first 11 minutes about 6 times now) to make sure everything is correct.

    I keep getting these errors

    https://i.imgur.com/lzzjabw.gifv



    Here is my PlayerController script

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. [RequireComponent(typeof(PlayerMotor))]
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     public LayerMask movementMask;
    9.     Camera cam;
    10.     PlayerMotor motor;
    11.     // Use this for initialization
    12.     void Start()
    13.     {
    14.         cam = Camera.main;
    15.         motor.GetComponent<PlayerMotor>();
    16.     }
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         if (Input.GetMouseButtonDown(0))
    21.         {
    22.             Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    23.             RaycastHit hit;
    24.             if (Physics.Raycast(ray, out hit, 100, movementMask))
    25.             {
    26.                 motor.MoveToPoint(hit.point);
    27.                 //Stop focusing any objects
    28.             }
    29.         }
    30.     }
    31. }
    32.  
    And here is the PlayerMotor
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.AI;
    6. [RequireComponent(typeof(NavMeshAgent))]
    7. public class PlayerMotor : MonoBehaviour
    8. {
    9.     NavMeshAgent agent;
    10.     // Use this for initialization
    11.     void Start()
    12.     {
    13.         agent.GetComponent<NavMeshAgent>();
    14.     }
    15.     public void MoveToPoint(Vector3 point)
    16.     {
    17.         agent.SetDestination(point);
    18.     }
    19. }
    20.  
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    you want :
    Code (csharp):
    1.  
    2. // first script:
    3. motor = GetComponent<PlayerMotor>();
    4. // second script:
    5. agent = GetComponent<NavMeshAgent>();
    6.  
    pretty sure that's what's wrong.
     
    iskippyrs likes this.
  3. iskippyrs

    iskippyrs

    Joined:
    Nov 23, 2017
    Posts:
    6
    Seems to have removed the errors, but the character is not moving, or well.. The cube.
    Any ideas?
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No.. lol

    Does the raycast work? Do you get a position? Have you baked the navmesh ?

    Just kinda difficult to guess what's not working ;) Sorry.
     
    iskippyrs likes this.
  5. iskippyrs

    iskippyrs

    Joined:
    Nov 23, 2017
    Posts:
    6
    The Raycast worked last teime I checked when I debugged the position to the console, I'll go ahead and try that again and baked the navmesh, I've done that too!
     
  6. iskippyrs

    iskippyrs

    Joined:
    Nov 23, 2017
    Posts:
    6
  7. iskippyrs

    iskippyrs

    Joined:
    Nov 23, 2017
    Posts:
    6
    Forgot to set the walking speed.. Lmao!
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sweet! Good find :) lol
     
    iskippyrs likes this.