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 /
This question was closed Jul 12, 2018 at 07:44 PM by Mateusz-Armir for the following reason:

I had a mistake in my scenethat I did not see, the whole question should not be asked. All works well.

avatar image
0
Question by Mateusz-Armir · Jul 12, 2018 at 04:33 PM · scripting problemcomponentinheritance

Get script with GetComponent, but as base class

Hello

I have a script ObjectInSpace that inherits from MonoBehaviour.

I hava a script MissileScript that inherits from ObjectInSpaceScript.

I have a script TestTargetScript that inherits from ObjectInSpaceScript.

I have a script AsteroidScript that inherits from ObjectInSpaceScript.

ObjectInSpaceScript has method dealDamage(float amount) that is public, so one ObjectInSpace can deal damage to the other.

MissileScript has method OnTrigger:

public void OnTriggerEnter(Collider c) { ObjectInSpaceScript target = c.gameObject.GetComponent<ObjectInSpaceScript> (); if (target ) { target.dealDamage (flightDamageMax, damageType); //deal dmg to target explodeMissile (); //missile dealt damage, so it can explode } else { Debug.Log ("No Script"); } }

I have a prefab in the world called Missile that has MissileScript.

I have a prefab in the world called TestTarget that has TestTargetScript.

I have a prefab in the world called Asteroid that has AsteroidScript.

Basically, all objects in my game world will have ObjectInSpace class as parent class.

The problem is:

When I deal damage to TestTarget or Asteroid, I want to get script component of ObjectInSpace and deal damage to it. The problem is, the components there are not ObjectInSpace - they are TestTargetScript and AsteroidScript. So I cant even get them to cast them to ObjectInSpace.

What am I doing wrong, how can I do this properly?

Comment
Add comment · Show 7
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 TreyH · Jul 12, 2018 at 05:13 PM 0
Share

I think you're doing it correctly. While I'm not sure what you mean by "the components there are not ObjectInSpace", they are by their definition. Copying your names:

 public class ObjectInSpace : $$anonymous$$onoBehaviour {
     public void DealDamage(float amount) {
         Debug.LogFormat ("DEALING DA$$anonymous$$AGE: {0}", amount);
     }
 }
 
 public class $$anonymous$$issileScript : ObjectInSpace {}


We can create a cube, attach a $$anonymous$$issileScript component, and observe how Unity understands these objects:

 public class Accessor : $$anonymous$$onoBehaviour {
 
     // Use this for initialization
     void Awake () {
 
         // This "missile" can be retrieved either way
         ObjectInSpace obj = this.GetComponent<ObjectInSpace> ();
         $$anonymous$$issileScript missile = this.GetComponent<$$anonymous$$issileScript> ();
 
         // And still has access to its ObjectInSpace-defined functions
         missile.DealDamage (1);
         obj.DealDamage (1);
 
         // Even though the compiler understands it to be a $$anonymous$$issileScript instance
         // AND
         // an ObjectInSpace instance
         Debug.Log (obj is ObjectInSpace);
         Debug.Log (missile is ObjectInSpace);
         Debug.Log (obj is $$anonymous$$issileScript);
     }
 }


That log will print:

 DEALING DA$$anonymous$$AGE: 1
 DEALING DA$$anonymous$$AGE: 1
 True
 True
 True


What weird behavior are you getting that leads you to believe inheritance isn't working in your case?


avatar image Mateusz-Armir TreyH · Jul 12, 2018 at 06:02 PM 0
Share

This is my code.

ObjectInSpace script file

  public class ObjectInSpace : $$anonymous$$onoBehaviour {
       public void dealDamage (float dmg) {
            Debug.Log ("Dealt "+dmg+" damage to "+gameObject.name);
        }
  }

$$anonymous$$issile script file

  public class $$anonymous$$issile : ObjectInSpace {
       public void OnTriggerEnter(Collider c) {
            ObjectInSpaceScript target = c.gameObject.GetComponent<ObjectInSpace> ();
            if (target) {
                 target.dealDamage (10f); //deal dmg to target
                 explode$$anonymous$$issile (); //missile dealt damage, so it can explod
             } else {
                 Debug.Log ("No Script");
            }
        }
  }

TestTarget script file

  public class TestTarget: ObjectInSpace {
       
  }

In the game world object Target has only TestTarget script.

In the game world object $$anonymous$$issile has only $$anonymous$$issile script.

When $$anonymous$$issile collides with Target it regiseteres hit. But when i ask to get the ObjectInSpace component, it cant to it, because the only script attached to the Target is TestTarget script, despite the fact, TestTarget inherits from ObjectInSpace.

In my case, Debug logs "No Script".

You added both scripts to game object, while I only add one.

avatar image ShadyProductions Mateusz-Armir · Jul 12, 2018 at 06:20 PM 0
Share
 ObjectInSpaceScript target = c.gameObject.GetComponent ();

This line doesn't make sense? ObjectInSpace cannot be of type ObjectInSpaceScript.

I literally just recreated this entire scene and it works fine for me.

Show more comments
avatar image Mateusz-Armir TreyH · Jul 12, 2018 at 06:33 PM 0
Share

Sorry, meant to be ObjectInSpace target = c.gameObject.GetComponent ();

1 Reply

  • Sort: 
avatar image
0

Answer by Mateusz-Armir · Jul 12, 2018 at 06:38 PM

This is my scene.

There is Missile on it. It only has MissileScript.

MissileScript inherits from ObjectInSpaceScript.

TestTargetScript also inherits from ObjectInSpaceScript and also is only script in TestTarget.

When Missile collides with TestTarget it tries to take ObjectInSpaceScript component, but it returns null, because TestTarget has TestTargets script that inherits from ObjectInSpaceScript.

How can I get MissileScript casted as ObjectInSpaceScript ?

alt text


unity.png (77.4 kB)
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

Follow this Question

Answers Answers and Comments

154 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 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 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 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 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 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

Inheritance and Component Types in C# 2 Answers

How to get different child components ( part) of character: get and set information 0 Answers

Get Script Component In Parent 1 Answer

Inheriting Parent Rotation and position 2 Answers

How to store data in script and attach it later? 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