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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by travellinguncle · Nov 15, 2014 at 01:08 AM · javascriptvariablesclassessubclassing

How do I access a script variable from a class defined within that script?

I'm pretty sure I'm being very dim here but I have a unity script which contains some script variables and also some classes. In the functions of one of the classes I want to access the script variables. I've simplified my script down to the following example that shows what I want to do:

 #pragma strict
 var testInt: int = 1;
 
 class TestClass {
     function testFunc() {
         Debug.Log("testInt is " + testInt);
     }
 }
 var testClass: TestClass;
 
 function Awake() {
     // Instantiate the class
     testClass = new TestClass();
 }
 
 function Update () {
     testClass.testFunc();
 }

but I get the error "TestVarClass.js(6,35): BCE0005: Unknown identifier: 'testInt'."

Is there a simple way to access testInt inside TestClass? I realise I could pass it in as a parameter but this is not practical in my actual script that is more complex than the above. Thanks in advance.

Comment
Add comment · Show 4
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 Linus · Nov 15, 2014 at 12:34 AM 0
Share

This sounds similar to a question I asked a while ago http://answers.unity3d.com/questions/288589/call-function-outside-class-in-same-script.html

avatar image travellinguncle · Nov 15, 2014 at 09:33 AM 0
Share

Thanks Linus, that page gave me some ideas. Thanks Jeff, I want the equivalent of testInt in my real script visible in the Unity Editor so I can tune it so I have it at the top level. There are also many instances of the class in an array (sorry, I stripped that bit out of my example) and I want a common value for each. It seems that parameter passing is the way to go to get the values into the class. I was trying to avoid this since in the actual script there are many values from the top level that I want to access but I just realised that I can make a little container class to hold all of them and then just pass that as a single parameter and dereference from that inside the class. I come from a block scoping background so it looked to me like it should have been simple to access but never $$anonymous$$d!

avatar image Kiwasi · Nov 15, 2014 at 09:41 AM 0
Share

This is because UnityScript does some weird class wrapping. Your top level variable is inside a class, just not one you have declared. One of the advantages of C# is you have much less stuff being auto implemented by the compiler.

I realise this may not actually be helpful, but if you are jus starting out switching may be viable.

avatar image Kiwasi · Nov 15, 2014 at 09:42 AM 0
Share

If you are after common variables you can use the static modifier from inside a class.

1 Reply

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

Answer by fafase · Nov 15, 2014 at 09:58 AM

One reason you would nest a class within another class is because you need to store data into a container for use within that class (Most of the time).

But the container should not be aware of the nesting class, it just doe its own thing. In here you TestClass trying to access a variable from the nesting class, though it may be possible via some hack, it also means you have a flaw in your design. A class is meant to do things about itself. So your printing should be in the parent class. If you need to access also info from the nested class then do it in the parent class:

 class TestClass
 {
    var data:int;
 }
 
 var testClass:TestClass;
 var info:int;
 function Start()
 {
     info = 10;
     testClass = new TestClass();
     testClass.data = 20;
     PrintOut();
 }
 
 function PrintOut()
 {
      Debug.Log(testClass.data + " " + info);
 }

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 travellinguncle · Nov 15, 2014 at 08:33 PM 0
Share

Thanks Fafase. As you suggest I have ended up using a container for the editor data in my actual script.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Cram variables inside class, use inheritance, or something else? 1 Answer

Should we always cache local variables in classes? 0 Answers

Custom classes having a strange behaviour 2 Answers

Linking prefabs to classes that are not attatched to gameobjects 1 Answer

Saving data to xml file (I have the loading down) 0 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