Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by jamal-dahbur · Oct 21, 2015 at 03:16 PM · c#arrayarrays

Convert string to array name?

is there a way to use a string as an array name? For example I have an array of game objects called Gems and another array called Rubys, in the player script is a string of what the player needs to catch that changes, so when that string is Rubys I want to spawn from the Rubys array and when it is Gems I want to spawn from the Gems array. Thing is I did a if(player.wordToCatch == "Rubys") { Instantiate(Rubys[Random.Range(0, Rubys.Length)], position, Quaternion.Identity); } else if(player.wordToCatch == "Gems") { Instantiate(Gems[Random.Range(0, Gems.Length)], position, Quaternion.Identity); }

but I have about four different arrays and my game is glitchy I feel like its from this, if I could just use the player.wordToCatch as the array name I'd eliminate so many lines of code. Like

 Instantiate(player.wordToCatch([Random.Range(0, player.wordToCatch.Length)], position, Quaternion.Identity);

but it won't work because obviously player.wordToCatch is a string, can I cast it or convert it or something so it knows I'm trying to reference that array?

Comment
Add comment · Show 3
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 Owen-Reynolds · Oct 21, 2015 at 04:57 PM 0
Share

Turning strings into variables is almost always a bad way to solve any problem. It's like asking how many loose bananas can fit in your car -- whatever it is you think you need to do that for, there's a better way.

In Unity, some people use tags, or attached scripts, to deter$$anonymous$$e what sort of item you've selected.

avatar image jamal-dahbur Owen-Reynolds · Oct 21, 2015 at 05:46 PM 0
Share

So do you think my current way is better performance wise?

avatar image Cherno · Oct 21, 2015 at 05:16 PM 0
Share

Use a Dictionary string-array or do it the hard way and use System.Reflection to find the right Field.

0 Replies

· Add your reply
  • Sort: 

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

33 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Remove values of one array from another C# 1 Answer

Why are my arrays in arrays null (converting from JSON)? 1 Answer

Index out of range error 1 Answer

Null reference exception in an if statement that checks for it. 1 Answer

Can't access public static array from another class 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