Can you execute C# from single string in game?

I want to know if it is possible to execute C# from a string in game (such as “save.health = 500” or “killDepth = -500” or "if (soulContainer.transform.childcount >= 0) {Debug.Log("Souls count: " + soulContainer.transform.childCound) " (I am pretty sure some errors would be thrown. but you should understand what I mean). I am trying to make a Command Console for in-game debugging and messing around, but doing case switch is a lot of work and errors (I currently got it ‘ok’ working but pretty sure things aren’t as great as they could be). Currently, it looks like this jumbled mess , and being able to just execute C# from a string with out having to program every little thing would be a huge time saver

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class CommandManager : MonoBehaviour {

    private GameManager gm;

    void Start () {
        if (GameObject.Find ("GameManager").GetComponent<GameManager> () == null) {
            Debug.Log ("Game Manager Not Found");
        } else {
            gm = GameObject.Find ("GameManager").GetComponent<GameManager> ();
        }
    }

    void Update () {
        CommandControl ();
        CommandInput ();
    }

    //CONTROLS TURNING ON/OFF THE COMMAND CONSOLE
    public void CommandControl(){
        if (Input.GetKeyUp (KeyCode.RightAlt) && gm.commandEnabled) {
            if (!gm.commandOpen) {
                gm.commandOpen = true;
                gm.commandConsole.SetActive (true);
                gm.ShowMouse ();
            } else if (gm.commandOpen){
                gm.commandOpen = false;
                gm.commandConsole.SetActive (false);
                gm.HideMouse ();
            }
        }
    }

    //CONTROLS INPUT OF COMMANDS
    public void CommandInput(){
        if (gm.commandOpen) {
            if (Input.GetKeyUp (KeyCode.Return) || Input.GetKeyUp (KeyCode.KeypadEnter)) {
                if (gm.commandInput.text.Length >= 1) {
                    gm.commandString = gm.commandInput.text;
                    gm.commandString.ToLower ();
                    gm.commandSplit = gm.commandString.Split (' ');
                    CheckCommand (gm.commandSplit);
                    gm.commandInput.text = "";
                }
            }
        }
    }
    //EASIER CHANGE COMMAND RETURN TEST
    public void ReturnCommand (string command){
        gm.commandReturn.text = command;
    }



    //ALL COMMANDS
    public void CheckCommand(string[] command){
        switch (command[0]) {
        case "killplayer":
            gm.KillPlayer ();
            ReturnCommand ("Player Killed");
            break;
        case "quit":
            ReturnCommand ("bye bye");
            gm.QuitGame ();
            break;
        case "save":
            ReturnCommand ("Game Saved");
            SaveLoad.Save ();
            break;
        case "restart":
            ReturnCommand ("Restarting");
            SceneManager.LoadScene (SceneManager.GetActiveScene ().name);
            break;
        case "hidemouse":
            gm.HideMouse ();
            break;
        case "showmouse":
            gm.ShowMouse ();
            break;
        case "sethealth":
            SetHealth (command [1]);
            ReturnCommand ("Health set to" + command [1]);
            break;
        default :
            ReturnCommand ("Unknown Command");
            break;
        }

        gm.commandInput.text = "";
    }

    //==============COMMAND METHODS===========================

    //SETHEALTH COMMAND
    void SetHealth(string health){
        if (int.Parse (health) >= 1) {
            gm.health = int.Parse(health);
        }
    }
       
}

(I don’t know the “proper” way of producing a GameManager, but I am using mine for storing all the main variables and GameObject references for easier access without having to create a lot more variables than needed.)

Let’s start with the easy answer: Use a singleton.

public class GameManager : MonoBehaviour {
public static GameManager instance;

void Awake() {
instance = this;
}
}
// anywhere else
GameManager.commandOpen = true;

As for the console, executing native C# code in the way you want ranges from “virtually impossible” to “actually impossible” depending on the platform you’re deploying to. And on top of that, opens up a whole can of worms in terms o security and stability. Best to do it the hard way.