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 GoThXoRd¹ · Feb 01, 2015 at 01:49 PM · variablestringname

String as Variable name

The OpenClose script will be on every door and there will be more than 3 doors/keys so I'm trying to use a string as a variable name from the CharInventory script.

The doors are not in order(the keys are named as example).

//Script: CharInventory

 var key1 : int = 1;
 var key2 : int = 0;
 var key3 : int = 0;
 

//Script: OpenClose

 var keyNeeded : String; //This will be different for each door( key1/key2/key3 )
 var charInventory : CharInventory; 
 
          //Everything between is fine
 
 function OnGUI(){
    if(GUI.Button(Rect(Screen.width / 2 - 25, Screen.height / 2, 50, 25), "Use Key")){
     if(charInventory.keyNeeded >= 1){  // <<<<<< How do I use the string to check CharInventory?
             //Unlocks the door
         }
         if(charInventory.keyNeeded <= 0){  // <<<<<< How do I use the string to check CharInventory?
             print("Need a key");
         }
     }
 }
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 Derek-Wong · Feb 01, 2015 at 02:43 PM 0
Share

this may need something related to Reflections if you need to check variable name, but i will suggest you to do it by using array

for 1, create an array in inventory script to hold the three variable, say:

public int[] keyArray = new int[3];

then in your openclose script set int for key needed:

int keyNeeded= 0; //this means need key1, key2 is 1 and key3 is 2

and check by:

if(inventory.keyArray[keyNeeded]>=1){...}

something like that.

avatar image GoThXoRd¹ · Feb 01, 2015 at 02:51 PM 0
Share

So how would you add a key to the inventory keyArray from another script?

avatar image Derek-Wong · Feb 01, 2015 at 02:55 PM 1
Share

say, add key to key1:

inventory.keyArray[0] += 1;

that's it~

You can also set a function in the inventory script:

public void get$$anonymous$$ey(int keyIndex){ keyArray[keyIndex] += 1; }

and an enum

public enum $$anonymous$$EYINDEX{ $$anonymous$$EY1, $$anonymous$$EY2, $$anonymous$$EY3, $$anonymous$$EYBOSS, $$anonymous$$EYTREASURE }

something like that, to help you on the arrayIndex and $$anonymous$$ey matching, so you don't need to remember what key is keyArray[0] anymore.

Now you can Call:

inventory.get$$anonymous$$ey((int)inventory.$$anonymous$$EYINDEX.$$anonymous$$EY1);

to get a key1

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

20 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

Related Questions

Is there a way to get the symbolic String name [editing name] of a variable at runtime? 1 Answer

transform.name value to a variable 2 Answers

Converting String to Variable Name 3 Answers

Does Unity have a "Nice Name" method? 1 Answer

How to edit string? How to add text 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