• 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
Question by spacepilot · Dec 16, 2011 at 02:30 AM · guivariablestringconvert

String to Var

My script shall provide a text-input-field in inspector to type in a variable-name.

How do I convert t$$anonymous$$s string to a variable I can use in my script afterwards?

Comment
Lo0NuhtiK
jahroy

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Eric5h5 · Dec 16, 2011 at 04:52 AM

Since you're using JS, you can use eval(), as long as it's not a webplayer or iOS/Android.

Comment
BiG
spacepilot

People who like this

2 Show 4 · 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 spacepilot · Dec 16, 2011 at 06:54 AM 0
Share

Can't find eval() in the documentation.

avatar image Eric5h5 · Dec 16, 2011 at 03:21 PM 1
Share

It's a language feature rather than a Unity feature.

 var n1 = 5;
 var n2 = 9;
 var var1 = "n1";
 var var2 = "n2";
 
 function Start () {
     eval("print (" + var1 + " * " + var2 + ");");
 }
avatar image spacepilot · Dec 17, 2011 at 08:47 AM 0
Share

Best answer up to now. Thank you Eric5h5.

avatar image Eric5h5 · Dec 17, 2011 at 02:52 PM 0
Share

I should amend the above; actually it works fine in a webplayer (was thinking of something else, sorry). It doesn't work on iOS because of the way code is compiled.

avatar image

Answer by jahroy · Dec 16, 2011 at 02:51 AM

You'll have to use reflection for that:

    http://www.csharp-examples.net/reflection-examples/

    I recommend restricting the user's input by presenting them with a list of choices, rather than trying to process their raw input. That being said, I don't really know what you're trying to do....

    It would be easier for us to help you if you tell us what you're trying to do.

    Comment
    karl_

    People who like this

    1 Show 9 · 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 spacepilot · Dec 16, 2011 at 04:21 AM 0
    Share

    I knew this answer would come. There is nothing more you can understand: The script is doing some math, someone can use for many purposes. To not having the need to open the editor, someone shall be able to define two input and one output-variable. I'm using javascript.

    avatar image jahroy · Dec 16, 2011 at 04:28 AM 0
    Share

    Well... If the user must type the name of a variable (not sure why that would be), then you'll have to use reflection.

    I have a feeling there's a much easier approach, but I don't understand what you're saying.

    My guess is a Hashtable, Dictionary, or Array is the solution to your problem.

    Are you familiar with these data types?

    You would use a Hashtable, for example, if you needed to keep track of a bunch of phone numbers and wanted to associate each one with a name.

    In that situation, the name is referred to as the "key" and the phone number is referred to as the "value". The keys and values of a Hashtable can be any type: ints, floats, strings, Transforms, GameObjects, etc...

    Maybe the values the user is entering into the TextField could be keys in a Hashtable or Dictionary.

    Not sure... Just trying to help.

    avatar image spacepilot · Dec 16, 2011 at 04:42 AM 0
    Share

    Which of the many reflection-examples is the proper one anyway? I know this answer was given in another threat already. The unity-documentation knows of many functions looking related. For instance (without knowing what is usefull):

    Gameobject.find

    Component.GetComponent

    Serializable

    Objects

    The Objects-page has the reverse-function: Object.ToString. Also, this answer claims to know a way to list variables which could be usefull to make a drop-down-list to choose from: How to create a collapsible variable list

    The variable-page in the unity-documentation says that all variables are member-variables by default which are available in inspector and even get saved with the project. So how do I access them from within inspector?

    avatar image jahroy · Dec 16, 2011 at 05:01 AM 1
    Share

    To access the variables of a script in the Inspector, you take the following steps:

    • create a script with some variables

    • attach the script to an object in the scene (drag/drop)

    • select the object from the scene (left-click on it)

    The Inspector is typically on the right side of the screen (otherwise you can get to it from the Window menu).

    When you select the object that has your script attached, you should see a slot in the Inspector for every public variable in your script (they're public by default).

    Depending on the type of the variable you can either edit it directly in the Inspector or drag/drop objects onto its slot.

    If you use the example below, you'll notice that you can edit the Color in the Inspector with all kinds of neat input methods. You can also drag a Mesh/Transform onto the slot named Some Transform.

    Here is a simple example script you could use:

     var someString     :  String;
     var someInt        :  int;
     var someFloat      :  float;
     var someColor      :  Color;
     var someTransform  :  Transform;
    
     function Update ()
     {
         /* make sure string isn't null before accessing it */
    
         if ( someString != null ) {
             Debug.Log("the string: " + someString);
         }
    
         /* ints and floats are never null - no need to check */
    
         Debug.Log("the int:    " + someInt);
         Debug.Log("the float:  " + someFloat);
    
         Debug.Log("the color:  " + someColor);
     }
    
    avatar image jahroy · Dec 16, 2011 at 06:58 PM 2
    Share

    @spacepilot

    Your best bet is to spend some time learning the basic principles of scripting with Unity. It is very very clear that you do not know what you want.

    This question is accomplishing nothing and should be deleted. Unity Answers is not meant to provide a tutorial zone for individual users. It's supposed to be a place where you can ask questions that will be useful to many people.

    We've written all this junk and I still have absolutely no idea what you're trying to achieve.

    If you want to quickly figure out the answer to your problem, you should provide a detailed, english description of what you want to do. Explain what the input from the user should be and what you'll do with it. If you don't want to tell us, it will be almost impossible for us to help.

    If what you're working on is a big secret, you'll have to either figure it out yourself or hire someone to help.

    Right now you're mis-using Unity Answers and won't get any help.

    Show more comments

    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.

    Welcome to Unity Answers

    If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

    Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

    Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

    Follow this Question

    Answers Answers and Comments

    6 People are following this question.

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

    Related Questions

    Inputfield set as Variable 1 Answer

    How can I Include string variables in Rich Text? 1 Answer

    Optimizing OnGUI 1 Answer

    Display additional text before the variable that the user is editing in a GUI Text Field 1 Answer

    Converting 'char' to the name of a variable 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