Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by bobor20001 · Sep 08, 2015 at 03:00 PM · strange ioc

How can child class trigger parent class function/void/event?

I have theese two example scripts:

Child class:

 using UnityEngine;
 using System.Collections;
 
 public class Health : MonoBehaviour
 {
     public void Die()
     {
         Destroy(gameObject);
     }
 }

Parent class:

 using UnityEngine;
 using System.Collections;
 
 public class Player_Health : Health
 {
     public void ShowDeadScreen()
     {
         FindObjectOfType<DeadScreen>().Show();
     }
 }

And now I want to child class trigger "ShowDeadScreen()" in parent class, how?

Please help, thanks! :)

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

1 Reply

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

Answer by DiegoSLTS · Sep 08, 2015 at 05:15 PM

A derived (child) class inherits all public methods from the base (parent) class. Just write the name of the method you want to execute.

You can make it an explicit call of the inherited method by using the "base" keyword, but it won't make a differente unless you've overrided the method: https://msdn.microsoft.com/en-us/library/hfw7t1ce.aspx

Anyway, I think you are confused. In your example, "Health" is the base (parent) class and Player_Health is the derived (child) class. ShowDeadScreen is a method of the derived class only, the base class don't have it and you can't call that method on it.

Also, an object of type Player_Health is also of type Health, but it's just one object, you can't call a method "on the parent" class, you call a method on the object itself, and the method is either defined on that class or inherited from the base class.

What are you trying to do exactly?

Comment
Add comment · Show 3 · 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 bobor20001 · Sep 08, 2015 at 07:46 PM 0
Share

In Health I need to execute parent Player_Health ShowDeadScreen() void in Die() void...

 public void Die()
 {
      Player_Health.ShowDeadScreen(); //I know it don't work like this but here I want execute parents ShowDeadScreen()
      Destroy(gameObject);
 }
avatar image DiegoSLTS · Sep 09, 2015 at 04:45 PM 0
Share

I guess you can cast "this" to Player_Health and then call the method. Something like:

public void Die() { Player_Health ph = (Player_Health)this; ph.ShowDeadScreen(); Destroy(gameObject); }

Anyway, that's a really REALLY bad approach, it's wrong in so many levels. A base class shouldn't call methods from the derived classes, it gos against the benefits of using polymorfism and inheritance.

Why not just make "Die" virtual, and override it in the derived class adding what you want? If Player_Health is derived from Health and Health is derived from $$anonymous$$onoBehaviour then Player_Health is a $$anonymous$$onoBehaviour and can be added as a component.

Change your scripts to:

      using UnityEngine;
      using System.Collections;
      
      public class Health : $$anonymous$$onoBehaviour
      {
          public virtual void Die()
          {
              Destroy(gameObject);
          }
      }

And:

  using UnityEngine;
  using System.Collections;
  
  public class Player_Health : Health
  {
 
      public override void Die()
      {
            ShowDeadScreen();
            base.Die();
      }
 
      public void ShowDeadScreen()
      {
          FindObjectOfType<DeadScreen>().Show();
      }
  }

And then add ONLY Player_Health to your player, then get the Player_Health component and call the Die method on it. It will show the dead screen then call de Die method implementation of the Health class.

avatar image bobor20001 DiegoSLTS · Sep 10, 2015 at 06:09 PM 0
Share

Thanks! Really helped! :)

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

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

Related Questions

How can I start to script an asset? 1 Answer

Can you develop on a consumer edition of an HTC Vive? 1 Answer

I need some scripting help. 0 Answers

Problem with UI Text 0 Answers

Json on cross plataform 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