Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
5
Question by DaveA · Aug 19, 2010 at 09:01 PM · prefabgetcomponentsubclass

Any way to 'GetComponent' as a base class?

Let's say I have a base class like 'animal' and 3 subclasess like 'dog', 'cat', 'bird' Let's say the base has a function 'speak' that all 3 subclasses implement (bark, meow, chirp).

Then these get added to prefabs.

Now, I'd like to have a script be able to access the script component and call 'speak' and whatever the subclass happens to be, it will make the corresponding sound. Follow?

So, can I just call gameObj.GetComponent(Animal).speak() ?? I would think Unity expects the exact class name (subclass), or GetComponent will fail.

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
7
Best Answer

Answer by Eagle32 · Aug 19, 2010 at 11:21 PM

In short I believe it (gameObj.GetComponent(Animal).speak()) should work, but it may not work with the non-typed versions of GetComponent like that. Here's what I've done that seems to work.

I've got a Component Damageable that extends Monobehaviour. From that I have 2 components that extend from Damageable. DroneHealth and ShipHealth.

DroneHealth and ShipHealth are attached to prefabs. In my collision test code I do

Damageable damagable = gotHit.transform.parent.GetComponent<Damageable>();

To get the component (the DroneHeralth or ShipHealth component that is, I never attach the Damageable component to anything).

I then call applyDamage, which has the signature public virtual void applyDamage(float amount, NetworkPlayer fromPlayer) in Damageable (and is overridden in the child classes) via RPC and the correct override in the subclass gets called (I was suprised this worked by RPC when I tested it, but it does. If it works that way I'm pretty sure it'll work for local calls).

Bare bones versions of the components

using UnityEngine; using System.Collections;

public class Damageable : MonoBehaviour {

 private float inVulnDuration = 0.0f;
 private float inVulnStart = 0.0f;

 protected bool damagable = true;
 public bool Damagable
 {
     get { return damagable; }
 }

 protected virtual void Update()
 {
     // if invulnerable 
     if (!damagable)
     {
         //check and see if we should become damageable yet
         if ((inVulnStart + inVulnDuration) &lt; Time.timeSinceLevelLoad)
         {
             damagable = true;
         }
     }
 }

 [RPC]
 public virtual void applyDamage(float amount, NetworkPlayer fromPlayer)
 {
     Debug.Log(": " + "applyDamage in Damagable ");
 }

}

using UnityEngine; using System.Collections;

public class ShipHealth : Damageable { protected override void Update() { base.Update(); // if invulnerable if (!damagable) { // code to make ship blink when invulnerable } }

 [RPC]
 public override void applyDamage(float amount, NetworkPlayer fromPlayer)
 {
         Debug.Log(": " +"applyDamage in shipHelath");
 }

}

Comment
Add comment · Show 2 · 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 DaveA · Aug 20, 2010 at 12:09 AM 0
Share

That's just what I needed, thanks!

avatar image Rodrigo Moraes · Nov 13, 2010 at 10:18 AM 0
Share

Although this answer was really helpful, it confused me a bit because of the RPC stuff which is not needed (as said but... well. I'm lost sometimes). All you need to do is:

gameObject.GetComponent();

Thanks anyway. :)

avatar image
2
Best Answer

Answer by Eric5h5 · Aug 19, 2010 at 09:08 PM

Sounds like you should be using SendMessage.

gameObj.SendMessage("Speak");

In that case it doesn't matter what the component is called.

Comment
Add comment · Show 6 · 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 DaveA · Aug 19, 2010 at 09:52 PM 0
Share

That may work, but my example above was simplified, I often need to set several variables, and Send$$anonymous$$essage can have only one param, correct? Any idea if you can send a structure as that param?

avatar image DaveA · Aug 19, 2010 at 10:17 PM 0
Share

Actually, Broadcast$$anonymous$$essage worked better for me. For some reason, even though objects using the SA$$anonymous$$E prefab, some got the message, some didn't, seems like a bug.

avatar image Wolfram · Aug 19, 2010 at 10:46 PM 0
Share

Send$$anonymous$$essage() is supposed to work only on the specified GameObject and its components, not any children or components connected to them; use Broadcast$$anonymous$$essage to send a message to all children

avatar image Eric5h5 · Aug 19, 2010 at 11:18 PM 0
Share

@DaveA: yes, you can make a class with several elements and send that.

avatar image DaveA · Aug 20, 2010 at 12:10 AM 0
Share

Good to know, thanks

Show more comments

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

1 Person is following this question.

avatar image

Related Questions

How do I assign a script to an instantiated prefab? 2 Answers

Get Component from Instantiated Prefab 1 Answer

Accesing script from newly instantiated game object 1 Answer

NullReferenceException when using GetComponent to access script variables 1 Answer

What should a script contain? 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