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
1
Question by Ariel J · Mar 15, 2011 at 08:15 AM · charactervariablestringconvert

Converting 'char' to the name of a variable

Hey, I've been searching around to find an example of what I'm looking for, but with no avail.

What I want to do is name a variable out of characters or using a string like this:

transform stuffA; transform stuffB;

void Start () { Do("A"); }

void Do (string test) { stuffB = "stuff" + test; }

Of course, the above doesn't work. The console tells me that it can't convert 'char' to a transform.

I've seen some stuff about Hash tables, but I'm unsure how to use them for this.

Thanks

-AJ

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by DaveA · Mar 15, 2011 at 08:22 AM

This depends on what you want Do to do. If you had a game object named stuffB you could GameObject.Find it like that. Otherwise, look into 'eval'

Comment
Add comment · Show 6 · 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 Ariel J · Mar 15, 2011 at 08:25 AM 0
Share

I actually want to name the variable, not find it. I'll have a look at 'eval' thanks!

-AJ

avatar image DaveA · Mar 15, 2011 at 08:29 AM 0
Share

Tho variables created with 'eval' are only in scope for the code passed to it per-invocation. So put as much code as you need to execute in that call. Beyond that, I don't know of any way to create variables on the fly

avatar image Ariel J · Mar 15, 2011 at 08:33 AM 0
Share

I don't need to create the variable, so this should be fine :)

avatar image Ariel J · Mar 15, 2011 at 08:45 AM 0
Share

Where can I find examples of eval? I have no clue how to use it :P

avatar image Owen-Reynolds · Mar 15, 2011 at 03:36 PM 0
Share

A hash table (or a map, or dictionary) can allow you to to pair up objects and names so you can look them up using a string/char. It's a much more common approach than directly converting a string into a variable.

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

String to Var 2 Answers

How to call a variable using a String for the variable name? 1 Answer

Converting a single String character to representative int. How? Javascript 2 Answers

How to Convert a Variable Name to a String? 2 Answers

Convert type `UnityEngine.Collider' to `string'? 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