Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

Trying to add structured, but unknown content/number, data and methods to a pre-existing object

Discussion in 'Scripting' started by Gatenus, Oct 6, 2021.

  1. Gatenus

    Gatenus

    Joined:
    Aug 31, 2021
    Posts:
    1
    I'm writing an educational planetarium simulator where users can click on a visible Star object instance to select it. A menu pops up where they can input a Myth to add to the Star - Myths consist of a Name, a Parent Culture, and a Narrative - then the Star adds that information to a List<Myth>.

    My Problem:
    When I try to create/edit the Myth for storage I get a 'null' reference, and I'm not sure how to implement my Myth class to fix it - nothing I've tried so far has worked (details in "c").

    My Thinking and What I've Tried So Far:

    a) I don't want Myth to inherit from Monobehavior since I don't need Myths to be their own GameObjects with all the extra data that comes with.
    b) I considered adding Myths to the Star as new components rather than a List<Myth>, but then it's harder to organize and access the individual Myths on a Star for later use (e.g. editing, display, etc).
    c) I've tried no inheritance, ScriptableObject inheritance, UnityEngine.Object inheritance, and various combinations of "new Myth()", Instantiate(), and CreateInstance<Myth>() (including nesting one inside the other just in case somehow that's the problem), but all I get is more 'null' errors. This is extra weird because for some of those configurations a Debug.Log(currentMyth) I use immediately after the declaration and attempted instancing outputs "(Clone) Myth" instead of 'null' on one line, but then I still get the 'null' error from an accessor call on the very next line.

    My Questions (and the thinking behind them):

    #1. Should I even be using a class to store the data in the first place or should I be using something else? The reason I've chosen to go this route so far is because I want the data to be structured - i.e. Name-Culture-Narrative all stored together (rather than using three separate Lists or arrays, one for each), the Myth must be instantiable since I know neither how many Myths each individual Star will end up with nor what data they will individually contain, and I would use structured Strings except I want to have methods/functions usable by instances of Myths (plus that feels like a dirty, ad hoc/idiosyncratic kind of solution).
    #2. If using a class like I'm currently doing IS the best way, then how can I "instantiate" a new Myth to add to my List<Myth> without getting 'null' and without requiring an entire GameObject come with it (assuming that's even possible)?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    21,614
    Fortunately it does not even remotely matter what you are doing.

    The answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. 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
    - also known as: Object reference not set to an instance of an object

    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.

    You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

    - drag it in using the inspector
    - code inside this script initializes it
    - some OTHER external code initializes it
    - ? something else?

    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.

    Here is a clean analogy of the actual underlying problem of a null reference exception:

    https://forum.unity.com/threads/nul...n-instance-of-an-object.1108865/#post-7137032
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    6,371
    I appreciate your details in your post, but you left out any code, which does make it harder to help you.
    I'm also not sure if you are wanting help fixing a null ref error or if you are trying to just discuss how to setup your "myth" data.

    If you need help with a null ref error, @Kurt-Dekker post should help. It honestly is as simple as the three steps.

    If you need help with how to setup your data, code that you're currently trying does help. Honestly, if it were something I was setting up, a json file stored on the computer or storing data on something like Firebase or other backend for retrieving (if I needed the data to be accessable from multiple locations) might be the way I'd go.
     
unityunity