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
1
Question by jamespaxi · Jul 25, 2013 at 10:38 PM · getcomponentinheritancesubclassing

Using GetComponent with a subclass

Hi all!

I have a Vehicle Script class with a Depart method.

I have a set of subclasses: Car, Motorcycle, Truck, Bus, ... each vehicle behaves differently when Depart is called.


A main method SendVehicle(Vehicle vehicle) is used to initiate and send a Vehicle.

Code is as follows

 var vehicleScript = vehicleInstance.GetComponent<Vehicle>();
 vehicleScript.Depart(...);

Unfortunately this calls Depart in the parent class, not the subclass!


Doing the following works perfectly fine!

 if(vehicleType == Vehicles.Car)
 {
     Car carScript = vehicleInstance.GetComponent<Car>():
     carScript.Depart(...);
 
 }


Any idea what I've done wrong?

The reason I'm using subclasses is because I would like to abstract this in the first place.


Hope someone can help! :) Thanks!

Comment
Add comment · Show 1
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 · Jul 25, 2013 at 10:56 PM 0
Share

I'm guessing you have both Vehicle and it's subclasses attached to the object. When using GetComponent the first thing that is searched for, is components that are of that type. The next thing that is searched for is a component that derives from that type. If you have both attached, it will only ever find Vehicle when searching for Vehicle.

It would be easier to answer if you gave us more information on the setup and the classes (not the content, just the setup ;) )

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Seth-Bergman · Jul 25, 2013 at 11:08 PM

I think you need a virtual function.. here's a few example answers:

http://answers.unity3d.com/questions/135194/how-to-use-different-types-of-scripts-with-an-over.html

http://answers.unity3d.com/questions/33477/question-about-overriding-functions.html

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
2

Answer by Jamora · Jul 25, 2013 at 11:12 PM

If you have an implementation of Depart in Vehicle and you do the first snippet, then you are calling the implementation of Vehicle.Depart, because you are handling Car as type Vehicle.

What you should be doing here is not provide an implementation of Depart in Vehicle at all, by making it an abstract class and providing an abstract method declaration in Vehicle.

 public abstract void Depart();

Now, you need to have

 public override void Depart(){/*implementation*/}

in all of your child classes for the second snippet to work as intended.

A slightly more complicated case

In case you have common functionality in Vehicle.Depart that all children need to do when their Depart is called, you need to instead have this in your Vehicle class

 public void Depart(){
 /*shared functionality*/
 SpecializedFunctionality();
 }
 
 protected abstract void SpecializedFunctionality();

and then you need to provide an implementation for SpecializedFunctionality in all your child classes as before.

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 Bunny83 · Jul 25, 2013 at 11:28 PM 0
Share

Abstract is actually a very special case where you want to force a subclass to implement this function and to prevent the creation of the base class itself. The "normal" way is to make the function virtual (abstract is a special kind of virtual) and you might override it in a subclass.

If you have shared functionality in the vehicle class you simply implement it in the Depart method of the base class. When you override the method in a subclass you can use base.Depart to call the Depart method of the base class.

avatar image
1

Answer by Bunny83 · Jul 25, 2013 at 11:54 PM

Just to provide a full example:

 public class Vehicle : MonoBehaviour
 {
     public virtual void Depart(string someParameter)
     {
         Debug.Log("Vehicle:Depart("+someParameter+")");
     }
 }
 
 public class Car : Vehicle
 {
     public override void Depart(string someParameter)
     {
         base.Depart(someParameter);
         Debug.Log("Car:Depart("+someParameter+")");
     }
 }
 
 public class Truck : Vehicle
 {
     public override void Depart(string someParameter)
     {
         // We don't need / want the base function in this subclass, so we don't call the base method
         // base.Depart(someParameter);
         Debug.Log("Truck:Depart("+someParameter+")");
     }
 }

 public class Bus : Vehicle
 {
     public override void Depart(string someParameter)
     {
         // The bus need to do some more stuff before calling the base method:
         Debug.Log("Bus:Depart("+someParameter+")");
         base.Depart(someParameter + " with changed parameter");
     }
 }


Some examples:

 var vehicleScript = carInstance.GetComponent<Vehicle>();
 vehicleScript.Depart("Sample one");
 
 vehicleScript = truckInstance.GetComponent<Vehicle>();
 vehicleScript.Depart("Sample Two");

 vehicleScript = busInstance.GetComponent<Vehicle>();
 vehicleScript.Depart("Sample Three");



This would print those lines:

     Vehicle:Depart(Sample one)
     Car:Depart(Sample one)
     
     Truck:Depart(Sample Two)

     Bus:Depart(Sample Three)
     Vehicle:Depart(Sample Three with changed parameter)
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

19 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

Related Questions

Does Unity support multiple Inheritance? 3 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How can I access an inherited method from a separate (collided) object? 1 Answer

Does Unity support Inheritance and Subclassing? 9 Answers

Inheritance and using GetComponent 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