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

Returned data getting null on the instantiated scriptable object

Discussion in 'Scripting' started by gabrimo, Nov 22, 2018.

  1. gabrimo

    gabrimo

    Joined:
    May 2, 2015
    Posts:
    86
    Hi all, don't know why this is happening but, I have an SO instance that has a property, which returns a dictionary. When I set an empty SO variable with Instantiate() using that SO, the returned data is getting null on that instantiated SO. Something like this:

    Code (CSharp):
    1.  ....
    2. instantiatedSO = Instantiate(originalSO);
    3. Debug.Log(originalSO.returnDictionary);  //This is one shows the data in the console
    4. Debug.Log(instantiatedSO.returnDictionary);  //This is one presents NullReferenceException or something
    5. ....
    I have no clue how to solve this, never used Instantiate before. Does Instantiate() resets the state of the original object's variables or something?
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,896
    Instantiate clones the object, to do this it uses the serialization system to copy the object, the serialization system does NOT support dictionaries. so the dictionary will be null. Basically if a public property does not appear in the inspector then it can not be serialized and so will not be copied using Instantiate.

    You can work around this issue. This example shows a way to serialize a dictionary https://docs.unity3d.com/ScriptReference/ISerializationCallbackReceiver.html
     
  3. gabrimo

    gabrimo

    Joined:
    May 2, 2015
    Posts:
    86
    Thank you sir, after read that doc, Im not sure that I understand the whole idea of that interface. I mean, after try to implement it in my project, I believe to not be using it right.
    Here's my thought, convert the original dictionary to that pair of lists and return that lists when needed or return the original dictionary itself. This doesnt sound like the right use of it, otherwise why Im using that interface instead of simply create my own methods for this convertion..

    Its worth mentioning that Im kinda new to coding, so I may have some newbie doubts/issues to understand this whole thing straight.