Problem with ScriptableObject

Hello guys, (sorry for the english I am franch)

I have a pb with ScriptableObject.

I have a ScriptableObject :

[CreateAssetMenu(menuName = "SO Generator/Caracteristique")]
public class Caracteristique : ScriptableObject
{
    // public int caraID;

    public enum CaraName{
        Force,
        Intelligence,
        Volonte,
        Endurance,
        Chance,
    }

    public CaraName  caraName;
    public string description;
    // public Sprite sprite;
    public int currentValue = 5;
    public int maxValue = 999;
}

and a classic C# object who use the ScriptableObject :

[System.Serializable]
public class PlayerModel
{
    public int level { get; set; }
    public float currenthp { get; set; }
    public float maxhp { get; set; }
    public float currentmp { get; set; }
    public float maxmp { get; set; }
    public float currentxp { get; set; }
    public float maxxp { get; set; }
    public int skillPoints {get; set; }
    public int statPoints {get; set; }
    public List<Caracteristique> playerCara;   
}

I have a warning when I instantiate my C# object ;

PlayerModel pm = new PlayerModel();

The warning :

I know it’s because of the ScriptableObject field in my C# object.

public List<Caracteristique> playerCara;

I know normaly for ScriptableObject we need to instantiate like this : ScriptableObject.CreateInstance()

But I can not instantiate my C# object like this.

What I got wrong ?

I’m rather confused as to why you’d be getting this error. A List is not an X and doesn’t have the same restriction, and your class doesn’t even initialize it on top of this, so near as I can tell it shouldn’t be throwing that warning. I tried creating an equivalent scenario and get no similar warnings. So I suspect you are looking at the wrong code or have posted the wrong version of the code. My only guess as to why it could be occurring legitimately is if there was some weird editor interaction going on here as my tests without the editor much involved didn’t produce the warning.

However, let’s say you do have normal class X which on creation does need to create a class Y which is a scriptable object. For this you would need to override your constructor and have in the constructor it use the CreateInstance method instead of new.

Thanks for the answer, I will exaplain a bit more.
I have my object PlayerModel who has a field List, Caracteristique is a ScriptableObject.
In another class I initailize PlayerModel like this :
PlayerModel pm = new PlayerModel();

I konw the warning comme from :
public List playerCara; in PlayerModel beacause the warning appear after I had this line. When I remove this line the warning disapear. (I have 2 other ScriptableObject who are also a List in PlayerModel and I have also a warning for them)

If you need more explanation tell me

I understand what you’re doing, but I can say with a strong degree of certainty that this is not the issue.
I can copy your scripts exactly and drop them in and I don’t get the warning. So I’m very confident you’re looking at the wrong code, or maybe you’re using an out of date version of unity that is improperly giving a warning in this circumstance. Like I said, a List is itself not a scriptable object even if x is a scriptable object, so it should have zero issue with you using standard constructors on it, and you’re not even using a standard constructor on it, you’re leaving it at the default unset value.

1 Like

I know it’s stange, I an other part of the game I do :
List caras = new List(); without the warning.

Just to be sure, did you run the game ? in my case the warning appear when I run it.

I will also test an a new project with just the C# class, scriptableobject.

My Unity version is quite recent it’s the 2019.4:3f1 the last reale is the 2019.4:5f1. (I need to update you think ?)

Ok I try on a new project I don’t have the warning.
And I try again on my real project and the warning don’t appear any more :eyes: but I change nothing …

Peraphs it was juste a bug of Unity
Thanks for the help, I will post again if the warning appear again.

For the future, note that this is the 2D forum for 2D features. This would be more appropriate in the scripting forum.

You’re more likely to get specific help on this there too. :slight_smile:

Sorry it was my first post, I will post corectly the next time

Not a problem at all! I just wanted to direct you to somewhere where you might get better answers! :slight_smile:

1 Like

How can I close the tread ? I don’t find

No need, it’s fine here.

My pb disapear by itself so I need to close the tread ( I think ?)

The pb come back and now I know why.
At a moment in my game I save my “PlayerModel” object and the List of scriptable object in a json.
When I deserialize the json it’s where it trigger the warning.

Someone know why ?

The code I use to deserialize the json :

var fileContent = File.ReadAllText(STATUS_FILE_PATH);
var jsonStatus = JsonConvert.DeserializeObject<PlayerModel>(fileContent);

The waring is in the jsonStatus line

My guess is internally, the deserializeobject is completely unaware of scriptable objects so it is using a new call for anything in the playercara list. Getting around that is going to be annoying as you’ll need a mirror of Caracteristique that is not a scriptableobject and you replace in your json recording only to move back to Caracteristique when you restore it. However, I suspect this is actually an indication of bad design. You really shouldn’t be recording scriptable objects. I know in my own game I make heavy use of scriptable objects but never need to record them to json. Whenever I need some kind of link between a recorded value and a scriptable object I use an index identifier instead. For instance I have building data which the player can create, and scriptable objects for the building parts. and the building data identifies what part it is by an index number. So when I save the game, I never need to save the building parts, they are instead built into the game itself.

1 Like

I resoleve the pb, I use a “clone” of my SO with the same field. When I load the game a take the value of the SO list and put them in this object, like this I can save them in json.
For now I don’t realy use the SO “in game” there are here just for the initialization of the game (the json file take the lead if it exist)

Thank for the help