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

(Basic question) NullReferenceException

Discussion in 'Scripting' started by ZorvE, Aug 11, 2022.

  1. ZorvE

    ZorvE

    Joined:
    Apr 18, 2022
    Posts:
    2
    Hi, iam new to Unity and C#. And such i try to grasp the basics, but i have a hard time getting classes to inherent variables from each other. For example look below.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TheOne : MonoBehaviour
    6. {
    7.     public int theVariable = 1;
    8.  
    9.     void Start()
    10.     {
    11.      
    12.     }
    13. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TheTwo : MonoBehaviour
    6. {
    7.     TheOne theOne;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         Debug.Log(theOne.theVariable);
    13.        
    14.     }
    15. }
    When i play test i get the error message below.

    NullReferenceException: Object reference not set to an instance of an object
    TheTwo.Start () (at Assets/Scenes/TheTwo.cs:12)

    I cant see what i am doing wrong, as far as I can see i give the int variable a value, but i does not transfer over to the other class.

    What am i doing wrong? :)
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    You need a reference to an actual "TheOne" object, just declaring the variable doesn't mean it's set to one. Since we're using Unity, you can actually just drag the component into the field in the Inspector, but normally in C# you would have to assign it to something first or create one.
     
    ZorvE likes this.
  3. ZorvE

    ZorvE

    Joined:
    Apr 18, 2022
    Posts:
    2
    Thank you! :)

    If someone else facing the same problem here is how i changed the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TheOne : MonoBehaviour
    6. {
    7.     public int theVariable = 1;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.        
    12.     }
    13. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TheTwo : MonoBehaviour
    6. {
    7.     TheOne theOne;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         theOne = new TheOne();
    13.         Debug.Log(theOne.theVariable);
    14.     }
    15. }