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 Joeri · Mar 05, 2013 at 07:43 PM · javascriptstringclassaccessing

How to access var in other class using string

I'm trying to access a variable in a different script using a string specified in the inspector:

 //PSEUDOCODE
 objective = myGameManager."objectiveToPick"; 

But this obviously doesn't work and I don't know how to pass the string as a variable name and use it to acces the property in the manager script.

I found methods using Hashtables, Dictionary's etc but they don't work and they seem above my current level of scripting.

  1. Is it possible to do this and if so, how?

  2. Are there any easy methods to do so?

  3. Does this impact performance a great deal?

What I need it for:


I am creating a object that displays a text above a npc. The message differs based on whether the objective was completed or not. The objective is a boolean inside a manager script. I want to use this script on multiple objects and check for different objectives.

I want to assign the objective variable in the inspector as a string then acces it using a reference to the gameManager script. So I declare the required variables specify the objective in the inspector and try the following in the start function:

 // PSEUDOCODE
 
 public  var objectiveToPick : String;

 private var myGameManager     : sc_GameManager;
 private var objective        : boolean;
 
 
 function Start (){
  myGameManager = GameObject.Find("GameManager").GetComponent(sc_GameManager); 
  objective = myGameManager."objectiveToPick";     // doesn't work, but I want to specify the variable using a string
 }

 function Update(){
     if(objective == false)
         Debug.Log("Objective isn't completed");
     else if(objectiveTo == true)
         Debug.Log("Objective completed");
 
 }

I'm using unityscript but feel free to post any C# answers. Thanks in advance

Comment
Add comment · Show 2
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 Joeri · Mar 06, 2013 at 12:34 AM 1
Share

Found a way to solve the problem using the answer in this thread:

http://answers.unity3d.com/questions/207095/construct-variable-name-from-strings.html

It uses reflection though and thus is slow. But since I only use it when entering a trigger it seems usable enough.

avatar image hoy_smallfry · Mar 06, 2013 at 12:35 AM 0
Share

Can you mark the question as closed?

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

11 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

Related Questions

Is there a way to instantiate a class, when its classtype is stored as a String variable (in unity-javascript)? 0 Answers

Create an array of classes, of different type. 1 Answer

Need assistance with SubType in custom class (UnityScript or C#) 2 Answers

Class Casting US/JS - 'foo' is not a member of 'Object' 1 Answer

GUI set max amount of characters for Label 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