Initialise AotList value using float array in ScriptableObject

KubaBaku 2 years ago updated 2 years ago 3

I made a custom ScriptableObject class to store some values of character (HP,MP,etc.),

but after I initialised the AotList value in the GameObject,

the value of them changes at same time, and they even affects the value of the SO used to initialise! (The changed values are saved into the SO)

I asked at Discord and someone told me try not to use the method above to initialise, then i tried to write a method like this:

public void Initialise(GameObject go, string characterID)
float[] status = Resources.Load("Characters/" + characterID).status;
float[] statusMax = Resources.Load("Characters/" + characterID).status;

Variables.Object(go).Set("status", status);
Variables.Object(go).Set("statusMax", statusMax);

...but the problem still occures :( Anyone have idea what's going on?

Bolt Version:
Unity Version:
Scripting Backend:
.NET Version (API Compatibility Level):
Satisfaction mark by KubaBaku 2 years ago

nothing to do with floats, someone just asked this. Check my be answer for why. It's legit behavior. https://support.ludiq.io/communities/5/topics/2355-variable-value-is-changing-when-i-using-get-variable


Hi KubaBaku!

As pointed out, I think this is just normal Unity behaviour with ScriptabeObjects you're seeing.

You're not copying the list (the float array) when using Set units, you're referencing the same list, the one that is stored on your scriptable object asset itself. That's why it's saved and shared.

The fact that you predeclared your variables as AotList has no impact here, because you're overwriting them entirely with the Set.

I can't think of a quick workaround right away without using a Create List + a For Each loop to populate it with new values. 

Oh! Thanks for the explanation and solution from JasonJonesLASM and Lazlo Boni!