• 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 Juice-Tin · Oct 10, 2012 at 09:34 PM · variablestringnumber

String + Variable to retrieve a variable?

Here's a quick Example of what I would like to do.

 string name1 = "John";
 string name2 = "Mark";
 int personNum = 1;

 Debug.Log(this["name"+personNum]); // Prints John
 this["name"+2] = "Mike"; // Changes Mark to Mike
 Debug.Log(this["name"+2]); // Prints Mike

 //John
 //Mike

So as you can see, I'm wondering if it's possible to form variables using strings/variables/ints for temporary use (setting or getting variables). This works in other languages and obviously 'this["variable string"] is not the correct C# syntax.

Note: I'm only trying to access pre-defined variables using this method, not create them.

Thanks.

Comment

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

  • Sort: 
avatar image
Best Answer

Answer by DaveA · Oct 10, 2012 at 09:42 PM

You could also create a Hash list and use the variable names as the keys.

Comment
Bunny83

People who like this

1 Show 3 · 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 Juice-Tin · Oct 10, 2012 at 11:42 PM -1
Share

That's true. But then the hash won't appear on the object script properties right?

Also I'm trying to store a bunch of sound effects in variables (through the public properties) and be able to easily access any with a string. Can't do that with arrays, and hash tables won't show up on property panel.

Thanks for the info though!

avatar image Eric5h5 · Oct 11, 2012 at 12:01 AM 0
Share

You can easily access sound effects in arrays. I would strongly recommend not using reflection and strings; it's quite slow and prone to error. If you want to use something "string-like" instead of numbers, use an enum.

avatar image Juice-Tin · Oct 12, 2012 at 05:55 PM 0
Share

Yeah thanks, I was just hoping for a easy way to add sound effects to a script via the property manager, and then be able to access them with simple strings (without having to manually place them in hashtables, arrays, etc).

Oh well, thanks for the info. I'll use reflection for minor things, and probably hash tables or arrays for larger, more time consuming processes.

avatar image

Answer by Juice-Tin · Oct 10, 2012 at 09:16 PM

Aha! Found it myself on some C# forums.

(string)this.GetType().GetField("name"+1).GetValue(this);

this returns the string variable called name1;

Comment
Bunny83
noonoox
ebfash

People who like this

3 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 Bunny83 · Oct 10, 2012 at 11:34 PM 2
Share

I'm actually impressed that you found the reflection way ;) Keep in mind that reflection is slow and should be avoided if possible since it breaks most OOP rules. Usually there are better ways of doing what you want to do.

The simplest way is to use an array

 public string[] name;

Btw: the variable name "name" could be a problem since "name" is already a property of Component.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

variables as GUI 1 Answer

Edit Variables From Game! 1 Answer

Label dots in between numbers (currency formatting) 2 Answers

Snychronize player's name over Photon? 2 Answers

Converting String to Variable Name 3 Answers


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