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

Null Error

Discussion in 'Scripting' started by Emmy43262, Dec 28, 2019.

  1. Emmy43262

    Emmy43262

    Joined:
    Dec 28, 2019
    Posts:
    1
    Hi everyone!
    I'm new to coding in c# and unity in general. I tried to use the input action thing and got this weird error after using a youtube tutorial.
    NullReferenceException: Object reference not set to an instance of an object
    Movement.Awake () (at Assets/Movement.cs:10)
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6. public class Movement : MonoBehaviour
    7. {  
    8.     InputMaster controls;
    9.     void Awake()
    10.     {  
    11.         controls = new InputMaster();
    12.         controls.Player.movement.performed += ctx => Move(ctx.ReadValue<Vector2>());
    13.     }
    14.     void Move(Vector2 direction)
    15.     {
    16.         Debug.Log("test " + direction);
    17.     }
    18.  
    19.     private void OnEnable()
    20.     {
    21.         controls.Enable();
    22.     }
    23.     private void OnDisable()
    24.     {
    25.         controls.Disable();
    26.     }
    27. }
    28.  
    Does anyone have any idea?
    PS Sorry for any grammar mistakes
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Line 10 as referenced in the error is just a {, so you've made some changes.

    My guess is it points to line 12, but without seeing InputMaster, it's hard to say. So something that is called in line 12 is null. Probably Player is null is my first guess, especially since you're setting this up in Awake, it's possible Player has no value yet.