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?

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 : MonoBehaviour {
	public void DealDamage(float amount) {
		Debug.LogFormat ("DEALING DAMAGE: {0}", amount);
	}
}

public class MissileScript : ObjectInSpace {}

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

public class Accessor : MonoBehaviour {

	// Use this for initialization
	void Awake () {

		// This "missile" can be retrieved either way
		ObjectInSpace obj = this.GetComponent<ObjectInSpace> ();
		MissileScript missile = this.GetComponent<MissileScript> ();

		// And still has access to its ObjectInSpace-defined functions
		missile.DealDamage (1);
		obj.DealDamage (1);

		// Even though the compiler understands it to be a MissileScript instance
		// AND
		// an ObjectInSpace instance
		Debug.Log (obj is ObjectInSpace);
		Debug.Log (missile is ObjectInSpace);
		Debug.Log (obj is MissileScript);
	}
}

That log will print:

DEALING DAMAGE: 1
DEALING DAMAGE: 1
True
True
True

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


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 ?