- Home /
How do I inherit certain varibles and functions from another script.
using UnityEngine;
public class Projectile : MonoBehaviour { public GameObject explosionVFX;
public Weapon damage;
void OnTriggerEnter(Collider other)
{
Debug.Log(other);
Destroy(gameObject);
Instantiate(explosionVFX, transform.position, transform.rotation);
if (other.GetComponent<EnemyHealth>() != null)
{
other.GetComponent<EnemyHealth>().TakeDamage(damage);
}
}
}
I have another script called weapon that has damage varibles and function I need on my projectile. No matter what I do it won't work.
Answer by Kimimaru · Dec 16, 2018 at 02:52 AM
Right now your code is using composition rather than inheritance. Essentially, your projectile has a weapon, when you instead want your projectile to be a weapon. Change your Projectile
class to inherit from Weapon
instead of MonoBehaviour
:
public class Projectile : Weapon
I don't want it to be a weapon but just deal damage and have the daamge calculation that I put in place. I made a spell system but that was with rays. I thought if I put oncollision in my weapon class that would conflict to much. SO it just seemed is easier to put it in the projectile script.
The OnCollision
method can be virtual so you can override it in a derived class if you're trying to avoid conflicts. I don't know exactly how all your code is, but I simply gave the syntax for inheriting in C# since the question asked about inheritance.
Your answer
Follow this Question
Related Questions
How do I make a photography function? 0 Answers
How can i check and fire an event when the user look at specific object ? 0 Answers
Adding callbacks/events to an instantiated prefab 2 Answers
How do I make my projectile deal damage to a target it hit. 1 Answer
How can i make both two cameras to follow the player but only one with control on player ? 0 Answers