Input a string and the computer answers with a command?

Hi, everyone. Sorry about the title, I wasn't sure what to call this question. I am looking for a way to, if the user inputs "Audio.PlaySound(3)" to a text area, then the program will play sound three in array. I want the computer to intelegantly read text, in short. How would I do this?

Get your user to type in some javascript code, then use eval() to run it.

Put this javascript in your project, then try messing with the command string ...

var command = "Debug.Log(\"Hello World\")";

function Start()

Yes, much simpler than my previous C# answer, wish I knew javascript better!!

Intelligently read text...

Um... I'm sorry, but computers never intelligently read text...

Maybe you should look up some things:

IBM's Watson.

Text-based games (Cons of).

And that should help you...

Goto start>run> cmd.exe > now try to use normal speech to make the computer do things =).

Just making a point, sorry...

But I don't know how you'd get input from a user and use it as a command, I'd actually like to know, so +1 =).

If you wanted it to be really, really simple, couldn't you just check to see if the user has entered a certain string and then run a function based on that? Kind of like checking for a password? I doubt you would be able to do much more than that, unfortunately, with unity.

If you don't mind making your user type in actual code, you could use C# reflection to invoke methods on the fly, something like this:

using System;
using System.Reflection;

public class MyMethodInvoker
    public static void Invoke(string command)
        int iPeriod = command.IndexOf(".");
        int iParen = command.IndexOf("(");
        string typeName = command.Substring(0, iPeriod);
        string methodName = command.Substring(iPeriod + 1, iParen - iPeriod - 1);
        string param = command.Substring(iParen + 1, command.Length - iParen - 2);

        Invoke(typeName, methodName, param);

    public static void Invoke(string typeName, string methodName, params object[] methodParameters)
        Type audioType = Assembly.GetExecutingAssembly().GetType(typeName);
        MethodInfo methodInfo = audioType.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public);
        methodInfo.Invoke(null, methodParameters);

For your example, you could then call MyMethodInvoker.Invoke("Audio.PlaySound(3)") and it would parse the string, then look up the Audio type and call the PlaySound method on it. My simple parsing logic above (which could definitely use some error-checking!!) will only parse out a single string parameter and hand it to PlaySound, which could deal with the conversion to integer, or you could make the parser better and handle multiple parameters of different types.

I've tested the code above against this trivial implementation of the Audio class ...

public class Audio
    public static void PlaySound(string param)
        Debug.LogError("Here we are in Audio.PlaySound(" + param + ")");

For more information on this technique, just google "C# reflection".

Obligatory disclaimers ...

  • error checking code needed
  • this exposes all your global classes -- you would want to put your user-callable methods in a separate pre-compiled assembly
  • won't work with javascript
  • I believe System.Reflection works in a web build, I don't think it works on iPhone
  • probably overkill for what you want, but this sort of thing can come in handy at times