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

Resolved Must be instantiated using the ScriptableObject.CreateInstance method instead of new SO

Discussion in 'Scripting' started by Rauljl, Oct 17, 2022.

  1. Rauljl

    Rauljl

    Joined:
    Oct 1, 2018
    Posts:
    5
    Hi, everyone.

    I have a problem and the following message appears:
    "RemoteSO must be instantiated using the ScriptableObject.CreateInstance method instead of new RemoteSO"
    Basically I have a Scriptable Object that contains other Scriptable Objects. On the other hand, I have a JSON file with the same structure, which I deserialize in order to fill the RemoteSO data using the following code:

    RemoteSO auxRemote = ScriptableObject.CreateInstance("RemoteSO") as RemoteSO;
    auxRemote = JsonConvert.DeserializeObject<RemoteSO>(jsonRemote);

    Everything works fine, but I get Warning messages. What I can do?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,007
    You can't serialise or deserialise anything inhering from UnityEngine.Object, nor can you deserialise data and just 'assign' that to a Unity Object.

    Your data needs to be represented as a plain class/struct and that data has to be stored inside the SO itself.
     
    lyingturkey likes this.
  3. Rauljl

    Rauljl

    Joined:
    Oct 1, 2018
    Posts:
    5
    I know, but actually I have a complex SO with other SO inside. The error message is from this line JsonConvert.DeserializeObject<RemoteSO>(jsonRemote) because i suppose when you Deserialize an object, a new Object was created. Also, it works perfectly, but i don't know why the error message is showing.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,007
    Doesn't change what I said. You can't and shouldn't serialise out entire Unity objects, it's not supported. What might work in one situation will likely fail in other situations.

    Represent your save-able data with plain classes/structs, represent your runtime data with scriptable objects. Just sounds like a case where you need some serialisable surrogate classes.
     
    lyingturkey likes this.
  5. Rauljl

    Rauljl

    Joined:
    Oct 1, 2018
    Posts:
    5
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,007
    Which is pretty much what I said.
     
    lyingturkey likes this.