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 jack95pp · Jan 14, 2014 at 08:01 PM · javascriptvariablesstatic

call variable from string?

Hi everyone, i'm new to unity and javascript. since now, sorry for my bad english ;) .

i've the following script (called inventory) that's just a reference for the objects

  static var object1 = 0;
  static var object2 = 0;
  static var object3 = 0; 

and another script where i've an integer input (1,2,3). i need to know how to modify inventory.variables from this script something like

 inventory.(object+input) = 1;

there's a way to call a variable from a string + an integer value? or just a string?

thanks everyone :)

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
3

Answer by Fabkins · Jan 14, 2014 at 08:32 PM

In short you cannot do this in this fashion. What you are probably needing is either an array or hashtable.

This is an example of using an array:

 var myobject = [10,11,12];

 myobject[2]=5; // NOTE that the index of 2 is the the THIRD value
 
 Debug.Log(myobject[0]);
 Debug.Log(myobject[1]);
 Debug.Log(myobject[2]);

This is an example of using a Hashtable:

 var myrefs = new Hashtable();
 
 myrefs["object1"]=0;
 myrefs["object2"]=0;
 myrefs["object3"]=0;
 
 var lookupString: String;
 var indexNumber=2;
 
 lookupString="object"+indexNumber.ToString();
 
 myrefs[lookupString]=3;
 
 Debug.Log(myrefs["object1"]);
 Debug.Log(myrefs["object2"]);
 Debug.Log(myrefs["object3"]);

Read up on Arrays and Hashes on:

http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?

Note that Unity has two types of arrays and its sometimes confusing so be aware

Comment
Add comment · 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 fafase · Jan 14, 2014 at 08:38 PM 0
Share

Following this idea you could go for a dictionary which would use a similar pattern as you are showing in your example:

 var template = "object";
 var dict = new Dictionary.<string,int>();

 var tempString = template + input.ToString(); 
 dict[tempString] = value;

but I don't really see the need of complexity when array would make it easy.

avatar image Fabkins · Jan 14, 2014 at 08:42 PM 0
Share

I agree, although I added my post to include the use of a Hash, if you are trying to reference something by an integer index you should really be using an array.

avatar image jack95pp · Jan 14, 2014 at 09:32 PM 0
Share

thanks a lot, but what if i have static var myobject_one static var myobject_two ? what would be the best way?

avatar image Fabkins · Jan 14, 2014 at 09:42 PM 0
Share

Please refer to the first line of answer. You cannot reference different variables by their variable names. That's what arrays, hash, dictionaries were design for.

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

19 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

Related Questions

Static Variables 1 Answer

Accessing a js static var from a c# script 1 Answer

Cannot use Static var/ Unknown Idientifier 1 Answer

multiple instantiate object script help 2 Answers

On Clicked, On Released GUI Button ? 2 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