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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Object reference giving random errors

Discussion in 'Scripting' started by Panduhh, Nov 30, 2015.

  1. Panduhh

    Panduhh

    Joined:
    Aug 12, 2014
    Posts:
    11
    I have this weird issue where the console is throwing an error saying the classic "Object reference is not set to an instance of an object", when in fact it is.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class CameraMovement : MonoBehaviour {
    6.  
    7.     public Transform myTransform;
    8.     Quaternion rot;
    9.     float input;
    10.  
    11.     void Start () {
    12.         myTransform = GetComponentInChildren<Camera>().transform;
    13.     }
    14.    
    15.     void Update () {
    16.         if(myTransform != null) {
    17.             input += -Input.GetAxis("Mouse Y") * rotSpeed;
    18.             input = ClampAngle(input, yClamp.x, yClamp.y);
    19.            
    20.             rot.eulerAngles = new Vector3(input, myTransform.rotation.eulerAngles.y,myTransform.rotation.eulerAngles.z);
    21.             myTransform.rotation = rot;
    22.            
    23.             myTransform.position = new Vector3(transform.position.x, col.bounds.max.y - .25f, transform.position.z);
    24.         }
    25.     }
    26. }
    27.  




    It's not breaking anything, it's just annoying to see. Anyone else having this problem?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    If Unity says the reference is null, then the reference is null, no way around it. There are no circumstances when it's random. One common cause is that you have two instances of the script and didn't assign the reference on one of them. Also, it's suspicious in this case where you have a public variable that's assigned in the inspector, but then you also assign it in the Start function; you should do one or the other but not both. GetComponentInChildren<Camera>() could return null if there is no Camera component on the object or any children.

    --Eric
     
  3. DMSolace

    DMSolace

    Joined:
    Nov 30, 2015
    Posts:
    9
    When you use public transforms and assign them in the editor, you don't need to use GetComponent. I'm not sure if thats it or not though. Sometimes I've encountered this error because I pointed at a prefab instead of an object in the heirarchy and vice versa.
     
  4. Panduhh

    Panduhh

    Joined:
    Aug 12, 2014
    Posts:
    11
    Thanks for the information! I made it public to make sure that something was getting assigned to it. I had it private when it first started happening. I can't believe I didn't think that I accidentally put it on another object, I must have been very tired yesterday. I just found the object that wasn't supposed to have the script on it. I feel very stupid right now.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    As a note for the future, you can see private variables if you choose "Debug" view in the inspector from the little gear icon in the top right.
     
  6. Panduhh

    Panduhh

    Joined:
    Aug 12, 2014
    Posts:
    11
    Yeah, I know. I just like seeing things as public for a personal preference :)
     
  7. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    is the CameraMovement Script on the same gameobject as the Camera component ?