Search Unity

Need Help with Null Object refrence

Discussion in '2D' started by bryanaugst_unity, May 2, 2021.

  1. bryanaugst_unity

    bryanaugst_unity

    Joined:
    Apr 26, 2021
    Posts:
    6
    My character keeps appearing in the wrong place in a scene and I get:
    NullReferenceException: Object reference not set to an instance of an object
    AreaEntrance.Awake () (at Assets/Scripts/AreaEntrance.cs:20)

    Code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class AreaEntrance : MonoBehaviour
    {
    public string transitionName;
    // Start is called before the first frame update
    void Awake()
    {
    //if the area transition name is equal to what we've named the player, get transform position(located in inspector).
    //Grab Player's transform position and make it equal to where the Area Entrance position is.
    if(transitionName == PlayerController.instance.areaTransitionName)
    {
    PlayerController.instance.transform.position = transform.position;
    }
    CanvasScaler.instance.fadeFromBlack();
    GameManager.instance.fadingBetweenAreas = false;
    }
    void Start()
    {
    }
    // Update is called once per frame
    void Update()
    {

    }
    }
     
  2. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    pls always use the code tags when posting code:


    its a bit hard to tell which line has which number right now without the code tags, tho in anyway, it seems like at least one of your static <class>.instance values is null

    like CanvasScaler.instance or GameManager.instance

    I wonder, where are you setting these? if you are assigning your "instance" variables in another Awake method, keep in mind that the Awake of AreaEntrance might be called befor the Awake of CanvasScaler or GameManager

    in that case you could change it from Awake to Start in your AreaEntrance script, since Start only gets called after all Awakes are through