[SOLVED] Serialization

In a nutshell, does monobehavior include events?

I have a class that handles gui buttons for setting stat points - strength, dex, etc.

I thought it would more more concise, especially from an inspector view to have a serializable class for each set of stats. Example: The strength attribute will have two buttons, to increase or decrease the value, as with a text element displaying the value. All stats will have these features. So I thought of making a serializable class that contains datatypes for the two buttons and the text. I was hoping to be able to be able to assign onClick listeners to these buttons, with their appropriate methods, however I am confused as to what is going on under the hood. The listeners are not working, and because I cannot serialize monobehavior I cannot use awake() and the like. This is what my serializable class looks like.

using UnityEngine.UI;
    [System.Serializable]
    public class StatAttributs
    {
        public string Name;
  
        public byte MinValue = 2;
        public Button Increase;
        public Button Decrease;
        public Text TextValue;
  
        public StatAttributs()
        {
            Increase.GetComponent<Button>().onClick.AddListener(IncreaseStat);
            Decrease.GetComponent<Button>().onClick.AddListener(DecreaseStat);
        }
  
        private void IncreaseStat()
        {
            int newValue = int.Parse(TextValue.GetComponent<Text>().text);
  
            newValue++;
  
            TextValue.GetComponent<Text>().text = newValue.ToString();
        }
  
        private void DecreaseStat()
        {
            int newValue = int.Parse(TextValue.GetComponent<Text>().text);
  
            if (MinValue < newValue)
            {
                newValue--;
  
                TextValue.GetComponent<Text>().text = newValue.ToString();
            }
        }
    }

Is this possible to implement, or should I just load a class with all buttons and their respective text values?

You can use events (C# or UnityEvent) from a MonoBehaviour. They are also serialized.
Were you saying you don’t want to use monobehaviour or you can’t? I was confused at the wording there.

I was under the impression that I could not serialize an object that inherited monobehavior - Not sure where I got that. Anyway, when I do inherit monobehavior that inspector no longer displays the fields for the StatAttributs object. I just get an input field that does not take anything within my project. Looks like the attachment.

3042173--227890--inspector.jpg

I see, there may have been a misunderstanding on my part about how you were going about this.
I was thinking that you could replace your attributes class with an updated version that is a monobehaviour that you attach to the gameobject in the scene. From there, you add the events and the fields you wish to use.
That make sense? Apologies if I’m missing something on how you want to use this and this isn’t fitting for you.

If I understand you correctly, I was trying to avoid that. Because it will clutter the inspector, although I suppose that I could use the header attribute to “separate” each button and its respective fields. I was trying to get it to work with a serialization because my inspector would look like this:

3042181--227891--inspector.jpg

It is mostly for ease of use and cleans the scripts inspector up.

Well, I can totally understanding wanting a good work area.
I don’t completely know what you want to accomplish. You have some stats, that the player can adjust up/down, thereby setting + displaying the results… ?
Okay, what about if you have a monobehaviour class with the events, but a field with your serializable class that’s drag in there?
Honestly, that’s a partial guess that it will work; but you could try it.
I think using the attributes is also a pretty good idea, if it’s that organization that is your real issue.
Just some options to try/consider… See what you prefer.

I want to have an array of this serialized class for the above inspector look. I am trying to add a listener to each button in the serialized class on creation, however, a constructor wouldn’t work and Awake() or Start() does not work without monobehavior. However, you got my brain turning. I have my serialized class and added an Initialize method to it. Now All I do is iterate through the Attributes array and call the Initialize methods. It is working without issue, at least as of yet =) The simplest solutions hide in plain site!

Thanks man!

Sure thing, glad I could be a part of working it [through] :slight_smile: Enjoy your game.