- Home /
Question by
AjZecrom · Jun 12, 2017 at 09:13 AM ·
componentinheritance
Inheritance with Guns
I am making a shooter game, and have, currently, two classes: "RangedWeapon" and "Pistol." The code for each is:
public class RangedWeapon : MonoBehaviour {
public GameObject bulletPrefab;
public Transform bulletSpawn;
public void Fire()
{
Shoot();
}
private void Shoot()
{
// Create the Bullet from the Bullet Prefab
var bullet = (GameObject)Instantiate (
bulletPrefab,
bulletSpawn.position,
bulletSpawn.rotation);
// Add velocity to the bullet
bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 6;
// Destroy the bullet after 2 seconds
Destroy(bullet, 2.0f);
}
}
and
public class Pistol : RangedWeapon {
}
My idea is to override shoot to add features such as reload time and ammunition count. Both classes are WIP. But my main problem is that I can't assign the gun to a component. There used to be a "Gun" class, and it worked. But my several classes can't be assigned. Any help?
Comment
Your answer

Follow this Question
Related Questions
Game architecture best practices 3 Answers
Stats component - properties 0 Answers
Get script with GetComponent, but as base class 1 Answer
2D Animation does not start 1 Answer
Component Class Hierarchy 2 Answers