Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kapstok666 · Jan 19, 2015 at 07:43 PM · c#javascriptstringstatement

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!");
             }
         }
     }
 }

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zharik86 · Jan 19, 2015 at 07:53 PM 0
Share

For line:

  if(Command != null) {
   Command; //your variable "Command" is equal? $$anonymous$$aybe, Command="myCom";
  }
avatar image MakinStuffLookGood · Jan 19, 2015 at 07:58 PM 0
Share

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".

avatar image richyrich · Jan 19, 2015 at 08:08 PM 0
Share

Are you trying to create a delegate?

avatar image kapstok666 · Jan 20, 2015 at 09:00 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.:

http://wiki.unity3d.com/index.php/ExpressionParser

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kapstok666 · Jan 20, 2015 at 08:54 PM 0
Share

So I can't create a parser in a unity C# script?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges