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 Abysinian · Apr 08, 2015 at 12:33 AM · getcomponentinheritanceinherit

Inheritance and using GetComponent

Hi there,

I'm working on the damage system for my game and I have an Enemy abstract class which is inherited by all enemy type scripts, in this example it's the Snail.

I have my system set up so that when the collider of a weapon object of a player collides with an enemies collider and they are pressing the "Attack" button, it calculates the damage and reduces the enemies health accordingly.

To do this, the weapon is using:

 void OnTriggerStay2D(Collider2D other)
     {
         if (other.gameObject.tag == "Enemy" && Input.GetButtonDown("Attack"))
         {
             enemy = other.gameObject;
 
             if(character.alignment == "Artisan")
             {
                 mainStat = character.luk;
                 atkType = character.atk;
                 defType = other.GetComponent<Enemy>().GetDef();
             }
         damage = damageScript.CalculateBasicDamage(character.atk, mainStat, defType);
         enemy.SendMessage("EnemyDamaged", damage, SendMessageOptions.DontRequireReceiver);
         Debug.Log("Enemy Hit.");
     }

The problem I'm having is that when this part of the script runs, I get a NullReferenceException at the GetComponent line. The enemy being collided with has the Snail script attached which inherits from Enemy, so I thought I'd be able to use GetComponent to get Enemy and use the GetDef() method to return the Snail's defense for the calculation, but it doesn't work. If I replace "Enemy" with "Snail" in GetComponent it does work however.

This wouldn't be too big a deal if the above snippet didn't need to be applicable to every monster in the game, but it wouldn't be practical to have to make a gigantic if statement for every monster in order to get their defense stat.

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 DoTA_KAMIKADzE · Apr 08, 2015 at 01:48 AM 0
Share

Well it should work if your Enemy script implements GetDef function, does it?

P.S. What that says?:

 Debug.Log(other.GetComponent<Enemy>() == null);

If that says "False" then it is clearly the problem with GetDef() function.

2 Replies

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

Answer by Abysinian · Apr 08, 2015 at 02:33 PM

The problem turned out to be I had left the old Enemy script on the Snail from testing, so even though it was disabled, it was interfering with the Snail script that was on there. Removing the Enemy script solved the problem.

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
4

Answer by nonathaj · Apr 08, 2015 at 09:23 AM

You are properly using GetComponent in your script. GetComponent will properly find references to a parent class if a child class component is attached to the gameobject. However, GetComponent cannot find components that are not attached to the same object.

Are you sure that the Enemy (or Snail in this case) script is attached to the same gameobject that the collider is on?

If the gameobject with your Enemy script is a CHILD to the gameobject with the collider, you can use:

 other.GetComponentInChildren<Enemy>();

If the gameobject with your Enemy script is a PARENT to the gameobject with the collider, you may need to use:

 other.transform.parent.GetComponent<Enemy>();

or

 other.transform.root.GetComponent<Enemy>();

Alternatively, to find the script anywhere up or down the transform tree of that object.

 other.transform.root.GetComponentInChildren<Enemy>();
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

20 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

Related Questions

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

Inheritance vs RequireComponent -1 Answers

C# - How do you GetComponent of a child class that inherits from a specific class? 1 Answer

Organise scripts with the same name? 1 Answer

How can I access an inherited method from a separate (collided) object? 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