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 vik.vega · Jan 24, 2014 at 03:54 PM · classestypecasting

Classes and subclasses typecasting in unityscript

Hi !

Code first, so i don't get lost into useless specifications:

 class mydevice
 {
 var integrity:float=100;
 var deviceName:String;
 }
 
 class battery extends mydevice
 {
 var storage_power:float
 }
 
 class generator extends mydevice
 {
 var recharge_rate:float=25;
 }

Now, since my goal is to add mixed types of device subclasses (batteries and generators) into a list, in order to compute, for example, the sum of storage_power or recharge_rate

And I succeded doing that, creating a list of type "mydevice". But this way, i can only see in the inspector, the variables belonging to the "mydevice" class, but not the subclass variables (and that definitively makes sense).

My unelegant solution was to put an enum "tagging" the mydevice, and checking for it's original subclass

     for(var i=0;i<ship_devices.Count;i++)
     {
     if ((ship_devices[i].type == ShipDeviceType.Battery))
         {
             var typecast = ship_devices[i] as battery;                
             print("this is a battery, with storage = "+typecast.storage);
         }
     }

I'd like to know if there is a better method than this, which by the way surprisingly works in retrieving the subclass variables. Basically what i'm looking for, is being able to add different derived subclasses to a single list, without "flatting" them to the class they extend, without having to tag them or put enums just to mark a difference between them.

Thank you and have a good day !

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 Benproductions1 · Jan 25, 2014 at 07:35 AM 0
Share

Please indent code. Please leave proper spacing when writing code. Please use UpperCamelCase for class names. Please don't double up ()'s.

To check the type you can use the is keyword.

 if (device is Battery) {
avatar image vik.vega · Jan 25, 2014 at 03:58 PM 0
Share

Looks like keyword "is" doesn't belong to Unityscript, you may haven't noticed i'm using it because of bad indentation or such

avatar image Benproductions1 · Jan 25, 2014 at 10:38 PM 0
Share

In that case (Found this looking up the same problem for C#)

 if (device.GetType() == typeof(Battery)) {

But I still can't find where your using the is keyword...

avatar image vexe · Jun 20, 2014 at 06:45 PM 0
Share

if (device.GetType() == typeof(Battery)) is not the same as if (device is Battery) - But if (typeof(Battery).IsAssignbleFrom(device.GetType()) is.

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Classes and subclasses typecasting in unityscript 3 Answers

How to add the score in a quiz game with text input field and a button. 0 Answers

Wave manager that enables multiple gameobjects after X seconds? 0 Answers

Handling different combinations of enum selection 1 Answer

How to perform Explicit typecasting in UnityScript? 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