- Home /
Use a string as a statement
Hey guys, I am busy with a new script and I'm trying to execute a string as a statement. Problem is that I keep getting errors that I can't just execute it like this:
using UnityEngine;
using System.Collections;
public class TriggerTaget : MonoBehaviour {
public bool IsTriggered = false;
public string Command;
void Start () {
}
void Update () {
if (IsTriggered) {
if(Command != null){
Command;
}else{
Debug.LogError ("Command for TriggerTarget is missing!");
}
}
}
}
Does anyone know how to change this script in a way I can use the string as a statement??,Hello guys! Is there a way to execute a public string as a statement?
I made this script (C#), but it keeps showing errors.
using UnityEngine;
using System.Collections;
public class TriggerTaget : MonoBehaviour {
public bool IsTriggered = false;
public string Command;
void Start () {
}
void Update () {
if (IsTriggered) {
if(Command != null){
Command;
}else{
Debug.LogError ("Command for TriggerTarget is missing!");
}
}
}
}
For line:
if(Command != null) {
Command; //your variable "Command" is equal? $$anonymous$$aybe, Command="myCom";
}
What are you trying to do? You can't execute "Command" as if it were a method if that's what you mean. You could do something like
if (Command == "$$anonymous$$y$$anonymous$$ethod")
$$anonymous$$y$$anonymous$$ethod();
You'd of course have to define a void $$anonymous$$ethod named "$$anonymous$$y$$anonymous$$ethod".
I am trying to execute a C# line within the game itself. See it as a 'command prompt' inside the game but then in C sharp language.
@zharik86 the first line ' if(Command != null){
' is for checking that the string contains something, so that the string will only be executed if it's defined.
Answer by unimechanic · Jan 19, 2015 at 08:16 PM
I'm trying to execute a string as a statement
You can't do that in C#, you need a parser to evaluate the string, i.e.:
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
C# Variable 4 Answers
Can i give GetComponent a variabel instead of a scriptname? 1 Answer
Problem recognizing declaration of array of strings 1 Answer