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 treep78 · Feb 13, 2012 at 11:00 PM · variablesclasses

How do you assign the class of one varriable to another?

So I've got an instance where I need to create a local variable and assign it to be one of two variables, the problem is that both of those variables are different custom classes. So how do I make that variable equal to either?

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
0

Answer by DaveA · Feb 13, 2012 at 11:09 PM

The best way is probably to have a base class that you declare for all those, and then subclass from that your two different classes.

Ex:

 class A
 {
 }
 
 class B : A
 {
 }
 
 class C : A
 {
 }
 
 A myVar1;
 A myVar2;
 
 A myVar3 = new B();  or new C();
 
 myVar 1 = myVar3; // should work either way

but you may need to explicitly cast things at some point too.

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 treep78 · Feb 13, 2012 at 11:20 PM 0
Share

Thanks I'll give that a shot.

avatar image treep78 · Feb 13, 2012 at 11:25 PM 0
Share

Didn't work. When I tried it Unity got mad at me and said it wasn't expecting ':' after 'class B'

avatar image DaveA · Feb 13, 2012 at 11:26 PM 0
Share

You using JS? Then use 'implements' ins$$anonymous$$d of ':'

avatar image treep78 · Feb 14, 2012 at 06:37 PM 0
Share

It didn't like 'implements' but it liked 'extends', do you think that should work?

avatar image
0

Answer by aviose · Apr 18, 2012 at 11:08 PM

extends is the proper way to create a subclass in Unityscript. Dave's example would thus be the following in Unityscript:

 classA
 {
 }
 class B extends A
 {
 }
 class C extends A
 {
 }
 var myVar1 : A = new A();
 var myVar2 : B = new B();
 var myVar3 : C = new C();
 myVar1 = myVar3;

The only thing I would worry about with this is the fact that even if you do set most of the same variables, it MIGHT drop the data that is exclusive to class C if you put it into a variable marked as a class A variable. (at the very least it will cause functions to default to the class A version, so you will have to set up a virtual version of it to force it to use the function from the subclass) That said, all 3 of these are considered class A regardless of the fact that one is class B and another is class C....

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

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Accessing variables from seperate scripts 1 Answer

Missing Documentation on accessing Variables 1 Answer

Can I choose what to pass into .getcomponent<{VARIABLE}>(); 1 Answer

Class functions don't seem to be updating class variables 1 Answer

How to add a variable to a prebuilt class? 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