I’m following lecture 135 of the RPG Core course, where we are editing the scriptable object. I saw this has been asked a few times, but I did not manage to find any solution.
[CreateAssetMenu(fileName = "Progression", menuName = "Stats/Progression", order = 0)]
public class Progression : ScriptableObject
{
[SerializeField] ProgressionCharacterClass[] characterClasses;
public float GetHealth(CharacterClass characterClass, int level)
{
foreach (ProgressionCharacterClass progressionclass in characterClasses)
{
if (progressionclass.characterClass == characterClass)
{
return progressionclass.health[level - 1];
}
}
return 0;
}
[System.Serializable]
class ProgressionCharacterClass
{
public CharacterClass characterClass;
public int[] health;
}
}
What I’m trying to fix is that when I drag the scriptable object from the assets folder into visual studio, it gives me the result that I posted rather than the one Sam has in his video, where you can clearly see all the values and modify the asset file in visual studio. This is how it should have looked like when I dragged it into visual studio:
So what I read online is that mine looks different for some some saving issues that Unity has with the scriptable objects and some solutions talk about creating a custom inspector, but with my current level I have no idea what it is about. Thank you in advance for the help.
ok, so this is not a big deal, and it has to do with how Unity is storing the values in the asset, probably to save space or something
You health is an array of int, while Sam’s is an array of float. From what I can see, if it’s float values, they will store like Sam’s, but the int values seem to be packed into a byte string;
Your asset is showing 3200000064000000c800000090010000f4010000
split those into parts of 8 (8 bits per byte) and you get
32000000
64000000
c8000000
90010000
f4010000
These are 5x 8bit values - in hexadecimal format. We can read them backwards (byte-magic and stuff) so we’ll get
00 00 00 32
00 00 00 64
00 00 00 c8
00 00 01 90
00 00 01 f4
If we convert them to decimal, we’ll get
00 00 00 32 = 50
00 00 00 64 = 100
00 00 00 c8 = 200
00 00 01 90 = 400
00 00 01 f4 = 500
So as you can see, Unity is packing these int values, but if you were to change your type to float[] (like Sam’s) you will get the normal list like in the lecture
Thank you very much, I would never thought using an int rather than a float would have changed the way how unity saves it. And thank you also for the detailed explanation, it helped me to understand the reasons a bit more.
To be honest, I don’t really know why they did that. Perhaps it’s to save space, but I don’t know. I just took your code and put 1,2,3,4,5 in my health values and noticed the correlation. I have some experience with bytes and stuff, so it wasn’t too difficult to see the pattern