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 ina · Nov 07, 2012 at 08:01 AM · javascriptclass

Referencing subclass in Unity JS

Unity JS lets you declare a subclass, or a class inside of a class, but how do you reference the subclass?

class Bunny{
	var name:String="FooFoo";
	var ears:int=2;
	var color:Color=new Color(1,0,0,1);
	class Abilities{
		var speed:int = 1;
		var smarts:float = 0.3f;
	}
};

var bunny = new Bunny(); var bunnyAbilities = new Abilities(); // doesn't work

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by fafase · Nov 07, 2012 at 07:10 PM

It is possible to access your inner class though it does not make much sense to me. You would be using a set of variables and methods that would require one more level of dereferenciation. On the other hand, you cannot instantiate an object of the type of the inner class.

 class Outside{
     var integer:int;
         class Inside{
             var intInside:int;
             function Inside(){ //Constructor of the inner class
                 intInside = 10;
             }
         }
     function Outside(){ // Constructor of the Outside class
         integer = 0;    
     }
     var inner:Inside = new Inside(); //Create an object of type Inside
 }
 
 var obj:Outside = new Outside();
 // var objIn:Inside = new Inside(); //Error, type not found
 function Start () {    
     print(obj.inner.intInside);//Access but one more dereferenciation.
     obj.inner.intInside = 15;
     print(obj.inner.intInside);
 }

you can access the members of Inside but you need to dereference from the created instance inside the class.

You may have a proper reason for that and there may be some use for this feature. I just do not see it.

Comment
Add comment · 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
0

Answer by Montraydavis · Nov 07, 2012 at 04:54 PM

Separate your classes, and make a reference like so.

 class BunnyAbilities{
    var speed : float = 1 ;
 }
 
 class Bunny{
    var Abilities : BunnyAbilities ;
 }
 
 var bunny = Bunny ;
 
 function Start ( )
 {
    bunny.Abilities.speed = 4; //Change speed at start .
 }

I personally LOVE LOVE LOVE class, and use it probably more than any other structured code .

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 fafase · Nov 07, 2012 at 07:10 PM 0
Share

Using this way compare to my way, you can declare instances of both classes.

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

assigned function declaration in unity javascript for different types 1 Answer

Does the || operator always work in UnityJS retrieval? 1 Answer

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

Are Unity's JS OOP functions customized? 2 Answers

Closure does not work in Unity JS functions? 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