Inspector To Show New Component Based on Bool Value

I’m trying to enable different abilities in the main script and have a new script automatically added to the inspector. It only works in Runtime. How do I do it so that this is also shown in the inspector before running the game?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Sirenix.OdinInspector;

public class Character : MonoBehaviour
{
    public enum WalkAbility { enabled, disable }
    public enum JumpAbility { enabled, disable }
    [Header("Abilities")]
    [EnumToggleButtons]
    public WalkAbility walkAbility;
    [EnumToggleButtons]
    public JumpAbility jumpAbility;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (walkAbility == WalkAbility.enabled)
        {
            //We have a string holding a script name
            string ScriptName = "WalkAbility";
            //We need to fetch the Type
            System.Type MyScriptType = System.Type.GetType(ScriptName + ",Assembly-CSharp");
            //Now that we have the Type we can use it to Add Component
            gameObject.AddComponent(MyScriptType);

        }
    }

}

Easy way is to use ExecuteInEditor attribute, the good way would be to write custom inspector for abilities. The right way wold be to create single Ability script and enable/disable abilities using it’s custom inspector by adding or removing scriptable objects describing those abilities to/from a list in that script under the hood.

Sorry I’m new… I don’t know how to do it. Still getting a hang of it.

ExecuteInEditor is super easy. Add [ExecuteInEditor] attribute on top of your behaviour script and it will get it’s update and other events called both in play and in edit mode. But there migh be some rocks under the water. I mean, you probably don’t want everythin to run in edit mode, just some part of your code. If that’s not the case then probably it’s the right solution for you. At least for now.

Extending the editor is not that easy, but it really worth the effort especially if you’re working on that project no alone. Catch the tutorial: https://www.raywenderlich.com/7751-unity-custom-inspectors-tutorial-getting-started

Here’s one example of using scriptable objects to architect your app: https://unity3d.com/how-to/architect-with-scriptable-objects This is the best choice architecture-wise so if you doing big project and have plans to support and update it for a long time after the release, you’d better learn how to use those.

The scriptable object is basically an asset in project folder storing vars for you. You may create your abilities descriptions as assets and write a script what executes any ability provided with such asset. After that, write custom editor for that script wich allows you to select and assing to it buch of such assets. Hooray, now you have a character with a buch of abilities he can run!

Good luck with your game then

2 Likes

Thank you so much! I didn’t know about [ExecuteInEditorMode]. It works perfectly :slight_smile: