• 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 /
This question was closed Mar 27, 2015 at 09:58 PM by jpthek9 for the following reason:

Too subjective and argumentative

avatar image
Question by jpthek9 · Mar 26, 2015 at 10:08 PM · c#eventinheritancemonobehaviour

How do I call function in inhereted script?

Kind of like how Unity calls Awake() in a script inheriting from MonoBehaviour, I want to call Initialize() and Execute() in any scripts inheriting from Ability.cs. How can I do this in C#?

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by CorruptedTNC · Mar 27, 2015 at 12:56 PM

The function you're looking to call in the parent script must be public, then just call it as you would any other function.

 using UnityEngine;
 using System.Collections;
 
 public class Parent : MonoBehaviour 
 {
     public void ParentFunction()
     {
         Debug.Log("Parent function was called!");
     }
 }
 
 using UnityEngine;
 using System.Collections;
 
 public class Child : Parent 
 {
     // Use this for initialization
     void Start () 
     {
         ParentFunction();
     }
 }
 
Comment
roojerry

People who like this

1 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 jpthek9 · Mar 27, 2015 at 04:49 PM 0
Share

From other scripts :p

avatar image siaran · Mar 27, 2015 at 06:29 PM 1
Share

um, the commenter above answered your question - his answer contains 2 scripts, Parent and Child.

Although I'd like to amend his answer - the function you're calling in the parent script doesn't have to be public, it can also be protected if you're only calling it from a child class.

Or, if you mean 'how do I call function Foo(), implemented in Parent, when I have a reference to Child, and want to call it from Other?

Then you can do

 public class Other : MonoBehaviour {
 
 public Parent parent;
 public Child child;
 
 
 void Call(){
  //both of these methods work!
  parent.Foo();
  child.Foo();
 
 }

}

If you want to call a method Bar() that is only implemented in Child, though, it gets a little trickier.

You can either do something like

 Child c = parent as Child;
 if(c) c.Bar(); 

Or you could make an abstract Bar() method in Parent.

avatar image jpthek9 · Mar 27, 2015 at 06:57 PM 0
Share

I'm confused. I don't know if the child has Foo(), only that the parent has Foo(). Basically, I want to have a moddable ability system for each player that I can drag and drop certain scripts onto. Each ability will inherit from the class 'Ability' and will have the function Execute(). I don't know that the child will be called Child; it could be called FireBall, MagicMissile, etc. but the main thing is it has the function Execute() and I need to call Execute() from the GameManager; not from the child or whatever. It's okay; I actually got the answer here: http://gamedev.stackexchange.com/questions/97358/callback-on-derived-class.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

An OS design issue: File types associated with their appropriate programs 1 Answer

Making a static class derive from MonoBehaviour in C# 3 Answers

C# Conception - Hide inherited members and functions 1 Answer

[C#] Using Static Create() methods with inheritance and Monobehaviours? 3 Answers

Multiple Cars not working 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