C# Developer Console - How to take strings appart...

Hi, my name is Kevin and I’m not new to unity but have decided to stop using JavaScipt and learn C#… As I wasn’t all that great is JS that poses somewhat of a problem which leaves me wondering, how the heck I can “take a string appart”.

Here an example to make it simpler:

I have the following command: “load l001” wherein “l001” is the name of the level to be loaded.

How do I make 2 strings, one per word out of this… It would be quite important to keep it inside of the already existing void.

Here is my current code which works but is a bit ineffective in loading levels.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Console : MonoBehaviour {

    public InputField DevConsole;
    public Text DevText;
    string UserInput;

    void Start ()
    {
        DevText.text = "";
    }

    void Update()
    {
        if (Input.GetKey("return"))
        {
            InputAnalysis();
            DevConsole.text = "";
        }
    }

    void InputAnalysis ()
    {
        UserInput = DevConsole.text;

        if (UserInput == "test")
        {
            DevText.text = "";
        }

        if (UserInput == "cls" | UserInput ==  "clear")
        {
            DevText.text = "";
        }

        if (UserInput == "load" | UserInput == "level" | UserInput == "map" | UserInput == "scene")
        {
            DevText.text = "Please specify which level should be loaded.";
        }

        if (UserInput == "load l001")
        {
            DevText.text = "The specified level will be loaded.";
            Application.LoadLevel("l001");
        }

        else
        {
            DevText.text = "Input not valid.";
        }
    }
}

I hope you have a better idea for this.

Greetings out of Belgium,

Kevin

//Cached delimeter, default to split on SPACE
public String CommandDelimeter = " ";
....
String[] stringArray = YourInputString.Split(CommandDelimeter);

So basically I can make it split at every ASCII character I specify? Cool, thanks man that will propably help :slight_smile:

Could you not just have a variable ‘int level:’ and set it via an input field. Then use an if statement to load said level. Would be able to use scene manager for this too

Yes, would deffinetly easier and originally i just had a lot of buttons but i want to be able to acces the console ingame. So that is sadly not an option,

Just for information, I somehow figured out what i needed to do, but the post of NA-RA-Ku certainly heped.

Thanks for that :wink:

BTW, I’m so glad someone invented the Internet or i’d be screwed ^^