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. Dismiss Notice

Scriptable Objects returns a Null Reference

Discussion in 'Scripting' started by laveurdegoudron, Jan 8, 2021.

  1. laveurdegoudron

    laveurdegoudron

    Joined:
    Oct 10, 2017
    Posts:
    14
    Hello all,
    Let me describe the problem I'm encountering : I am working on a 2D RTS game, and I started using Scriptable Objects to "store" basic data about the available buildings (cost, time to build, sprite, etc).

    I created a sample Scriptable Object called "Barrel", in which I put some data (cost = 3, a sprite, etc). I then attached it to the BuildItems Script in the Inspector.

    I call these variables by a BuildItems C# script attached to a ResourceController GameObject. In Visual Studio, the code shows no error, but when I run the game in the editor, I get this error :

    NullReferenceException: Object reference not set to an instance of an object
    BuildItems.Update () (at Assets/Scripts/Building System/BuildItems.cs:18)

    I have looked on Google for a lot of topics, watched quite a lot of videos abut Scriptable Objects, and I know what a NullReferenceException is so I tried to solve the problem myself for hours.

    This is my code for the Scriptable Object :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [CreateAssetMenu(fileName = "New Build Data", menuName = "Building Data")]
    6. public class BuildingData : ScriptableObject
    7. {
    8.     public string buildingName;
    9.     public string description;
    10.     public int natureCost;
    11.     public int foodCost;
    12.     public Sprite sprite;
    13.     public int buildTime;
    14.     public int foodBonus;
    15.     public int natureBonus;
    16. }
    And for the BuildItems script :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BuildItems : MonoBehaviour
    6. {
    7.     [SerializeField] private BuildingData buildingData;
    8.  
    9.     private void Start()
    10.     {
    11.  
    12.     }
    13.  
    14.     private void Update()
    15.     {
    16.         if (Input.GetMouseButtonDown(0))
    17.         {
    18.             Debug.Log(buildingData.natureCost);
    19.         }
    20.     }
    21. }
    Can you tell me what I did wrong please ?
    Thank you, and best regards,
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    In all likelihood you did not initialize buildingData. Ensure that the script that gives you the error is the instance you think is being executed.
     
    Boodums likes this.
  3. laveurdegoudron

    laveurdegoudron

    Joined:
    Oct 10, 2017
    Posts:
    14
    First of all, thank you for your reply.
    I am not sure if that's what you meant, but at some point I tried to add CreateInstance in the void Start() but it did not help, since it was not taking in account the SO that I had dragged and dropped in the inspector.

    Is it what you meant or am I wrong ?
    Best regards,
     
  4. laveurdegoudron

    laveurdegoudron

    Joined:
    Oct 10, 2017
    Posts:
    14
    Also, on the tutorials I watched I do not think that people were initializing the SO, but I might be wrong.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    The answer is always the same... ALWAYS. It is the single most common error ever. Don't waste your life on this problem. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.
     
  6. laveurdegoudron

    laveurdegoudron

    Joined:
    Oct 10, 2017
    Posts:
    14
    Hello, thank you for your answer.

    I stated in my original post that I know what a MissingReference is, but on this particular case I was not able to fix it, like I have done in the past.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    1. how do you expect line 7 (buildingData) to be initialized? (something adds it in code? you drag it in via the editor? something else?)

    2. why is it not initialized (you didn't write init code? the init code didn't run? you did not drag it in in the editor? there are more than one copy of this script in scene, you only dragged one in? something else?)

    Understanding the above is not optional.
     
  8. laveurdegoudron

    laveurdegoudron

    Joined:
    Oct 10, 2017
    Posts:
    14
    1. I dragged the SO via the editor.
    2. As I was saying to csofranz above, I tried to add CreateInstance in the Start void. It stopped returning the NullReference but it did not take in account the values of the SO, it just felt like it was using a blank one. I couldnt debug.log the variables either.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Are you sure you don't have another instance of this script that you did not drag it into?

    In the hierarchy search for the scene or prefab type
    t:BuildItems
    and see how many there are, or else in your Start() use Debug.Log() to print the
    name
    of the GameObject this script is on.
     
  10. laveurdegoudron

    laveurdegoudron

    Joined:
    Oct 10, 2017
    Posts:
    14
    Hello,
    Thank you again for your answer. Well, that was it ! I would have never suspected that, I had another instance of the script attached to a UI GameObject... I don't know how I managed to do that. But it now works fine.

    Thank you so much.
     
    Kurt-Dekker likes this.