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 /
avatar image
0
Question by RaptopLob · Jun 24, 2011 at 05:46 PM · javascriptclassdynamic variables

Dynamic variables and references in a javascript class

Hi,

I'm trying to clean up some code which is repeated 4 times but uses different variable and references names. Its all nice organised so that it should be easy enough to feed a variable name into a class and let it insert the variable into the relevant parts of the code.

However I can't get round this:

Code which works:

 var storageC = resourceHolder.h2oStorageCurrent;

Code which DOESN'T work:

 var name = "h2o"; //this would actually be a variable defined during the call to the class, I just put it here to simplify the example
 var storageC = resourceHolder[name + "StorageCurrent"];

Obviously there is more to my class than that, but as I can make it work with the 1st bit of code, it must be my understanding of javascript and it's coding practices.

Hopefully you can see what I'm trying to do and suggest a solution.

Thanks

Rob

Extra Information

Apologies for not providing all the info you need to help. I am trying to pull a variable from another GO to use with the drawing of UI elements on this GO. My code works, but I have 4 blocks of very similar code I'm trying to clean up into a reusable class. The error I get is: "MissingFieldException: Field 'HUBResources.' not found."

Here is my full class: class statusPanel{

 var name : String;
 var offsetX : int;
 var offsetY : int;
 var resources : GameObject;  //resources is passed a GO which has a script called HUBResources attached to it. This contains some int and float variables which affect the whole game, but in this instance are just being read to display them on a pda style system.

 function statusPanel(name, offsetX, offsetY, resources, textHolder){
       var filename = "pda_icon_"+name+"_green.png";
   var resourceHolder = resources.GetComponent("HUBResources"); //Here I reference the script directly

       var storageC = resourceHolder.h2oStorageCurrent; //This is the working non-dynamic version
       var storageC2 = resourceHolder[name"+StorageCurrent]; //This is my attempt to make the same line dynamic so I can reuse the code for my other variables sets, oxygen, energy and food.

//The rest of the code uses UIToolkit to draw out various UI elements, all of which work fine.

   var icon =  UI.firstToolkit.addSprite(filename,  offsetX + 80, offsetY + 120);
   var bar =  UIProgressBar.create("pda_misc_bar.png",  "pda_misc_barHolder.png", 3, 3, 0, 0);
   pos = bar.position;
   pos.x = UIRelative.xPixelsFrom( UIxAnchor.Left, offsetX + 130 );
   pos.y = -UIRelative.yPixelsFrom( UIyAnchor.Top, offsetY + 120 );
   bar.position = pos;
   bar.resizeTextureOnChange = true;
   bar.value = storageC / resourceHolder.h2oStorageMax;    
 }
 

};

I'm pretty sure the problem is just contained within the storageC2 line.

Thanks again.

Rob

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 burnumd · Jun 24, 2011 at 05:51 PM 0
Share

You'll need to give us more information. What do you mean it doesn't work? Do you get an error, does something not behave the way you expect? If you're getting an error, what is the error? What type of object is resourceHolder?

avatar image RaptopLob · Jun 24, 2011 at 06:21 PM 0
Share

I've updated the question, thanks for helping.

avatar image RaptopLob · Jun 25, 2011 at 12:11 PM 0
Share

Can no one figure out what I'm trying to do? Perhaps it's not even possible?

2 Replies

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

Answer by RaptopLob · Jun 25, 2011 at 01:49 PM

I have resolved my problem by placing the following function within the GO I am requesting the variables from:

function returnValue(nameString : String){ return this.GetType().GetField(nameString).GetValue(this); }

I then call the function during the assigning of the local variable, like this: var storageCurrent = resourceHolder.returnValue(name+"StorageCurrent");

Comment
Add comment · Show 2 · 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 burnumd · Jun 27, 2011 at 02:45 PM 1
Share

Using reflection to get fields named with strings is going to get very expensive if you do that regularly. You might be better off setting up a System.Collections.Generic.Dictionary collection and hitting that. This approach has the added advantage that you can check to see if an object even contains a value with that key name.

avatar image RaptopLob · Jun 27, 2011 at 02:49 PM 0
Share

thanks burn, I'll look into it!

avatar image
0

Answer by Anxo · Jun 24, 2011 at 05:56 PM

I agree with Burn, you are using shortcuts by not declaring your objects so we don't know if your resourcholder and stuff is a game object or what.

Just remember if you are declaring something with: "" then you are making it a String. it would also help if you would tell us what happens that was not intended if anything or link the error.

From what you told us, I will guess that you are trying to call a gameObject with a string.

Comment
Add comment · 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 RaptopLob · Jun 24, 2011 at 06:21 PM 0
Share

I've updated the question, thanks for helping.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Public Class Error 1 Answer

Custom JS class error - function is not a member of the class 2 Answers

NullReferenceException: Object reference not set to an instance of an object 1 Answer

missingGameObject problem 3 Answers

How to access a variable within a multidimensional array of class 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