- Home /
Make inspector show changes of class variable value in function?
Hope you understood my title (question). This is a simplified version of the code to make the concept clearer:
using UnityEngine;
using System.Collections;
public class customClasses : MonoBehaviour
{
public CharacterAttributes playerAttributes = new CharacterAttributes();
public void Start()
{
playerAttributes.level = 12; //these two changes does now show
playerAttributes.name = "Cpt. Awesome"; //but i want these changes to show
}
}
[System.Serializable] //makes the class variables show in inspector
public class CharacterAttributes
{
public string name; //shows with value "" (this is correct)
public int level; //shows with value 0 (so is this)
}
So... i have successfully made the class variables show in inspector, BUT not the changes i perform in the top function "Start"
Changes made during the game aren't supposed to affect the Inspector's values. You could save/load on a file if you need to set things through gameplay, or you could expand the Editor itself.
What are you trying to do with this? $$anonymous$$aybe we can help more better if we know what you're trying to accomplish. (like "i want to set player spawn point by walking there" or something)
@Vicenti: Who said that? Of course changes made at runtime does affect the inspector and the hierarchy since at runtime you run on a copy of your scene.
Answer by Bunny83 · Aug 12, 2012 at 07:24 PM
Remove "= new CharacterAttributes();" from the declaration. It's useless since it'S a serialized variable and class so the Unity inspector will create the instance automatically. System.Serializable classes actually act more like structs ;)
Anyway you should see the changes in the inspector when you press play. Or when do you expect the changes?
edit
I've just tried your class and i see the changes when i press play. I'm not sure what behaviour you would expect...
Not sure what i was thinking yesterday :/ Anyhow, thanks a bunch. I never managed to get this far in my head :P
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
organize unity inspector with collapsable sections without using classes 1 Answer
Can't edit referenced class in the inspector 1 Answer
Accessing Normal Class from another class which derives from Monobehaviour 2 Answers
Why inpector keeps methods assigned when changed from public to private 2 Answers