- Home /
I had a mistake in my scenethat I did not see, the whole question should not be asked. All works well.
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 : $$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?
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.
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.
Sorry, meant to be ObjectInSpace target = c.gameObject.GetComponent ();
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 ?