Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do I change [SerilizedField] script property on the run?

Discussion in '2D' started by rizenmusic, Mar 23, 2021.

  1. rizenmusic

    rizenmusic

    Joined:
    Oct 2, 2019
    Posts:
    23
    Hi! I'm working on my State Machine. I've got my FSM script on an object, inside of it sits a [SerializedField] property of a current state.



    Like this. The question is how do I change it to another State script when it's needed? I have no Idea how to do that.
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    Can you rephrase the question? If you want to change the entire script ("change it to another State script") then you can have another controller script that can add and remove certain scripts. Also, why are you needing to use SerializedField for that? Are you changing the PlayerData via the inspector during runtime?
     
    rizenmusic likes this.
  3. rizenmusic

    rizenmusic

    Joined:
    Oct 2, 2019
    Posts:
    23
    What I want to achieve is to change behaviour state scripts for my AI during runtime via State Machine.

    For example, I have an FSM.cs controller that runs currentState, currentState.cs runs some code. Then, when certain conditions are met in this currentState script, I need FSM controller to stop running currentState, assign another State script as currentState and run it until the next conditions are met, then change it to another State and so on. Ideally I want this script to be on my FSM controller which is on my enemy gameObject, for example, either by adding it through "public State state" or "private [SerializeField]" so I can see what state it is running right now in the inspector.

    That is for my player controller and my enemy and boss AI - I'm trying to implement a Finite State Machine because I don't like spaghetty of "ifs" in my code much so I'd rather have states for those kind of things :)
     
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    Yeah i remember giving you some info on FSMs a while ago. So if you set up your states in an Enum and make that field a SerializedField, I believe you can see it in the inspector and change the states that way. It was an old project but i was able to have a drop down of my states and change them during runtime.
     
  5. rizenmusic

    rizenmusic

    Joined:
    Oct 2, 2019
    Posts:
    23
    Yeah, I already got my enum FSM) It's good for small things but it's not very modular, so I want each state to be in its own script which I can change through FSM controller on any gameObject I want.
     
  6. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    Ahhh i see. I have tried a tutorial on that a few years ago but had no luck with it so im afraid i am not sure. Try posting in the scripting forum, they are all the smart ones. Technically i think your original post should have gone to that forum anyways.
     
    rizenmusic likes this.
  7. rizenmusic

    rizenmusic

    Joined:
    Oct 2, 2019
    Posts:
    23
    Thanks for pointing that out, I'll check scripting forum, and thanks for intention to help)