Child class cannot access property in method?
I'm trying to make a simple script for firing a weapon that gets a set of shoot spawn points from a 2d object. Then, the player/enemy script will simply pass the direction they are facing to declare which spawn to fire from. The List is filled in the Start function (It was in the parent class, but I've been fighting this for a day). In the debug code, the List is NULL when the Shoot() method is called. I don't understand why. On top of all this, it says the object reference is not set to an instance of an object.
Thanks for any help, this is my first serious attempt at a prototype, and it's going fine, otherwise.
Code as follows for the abstract and child class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class Weapons : MonoBehaviour {
protected float damage;
protected float speed;
protected float shootDelay;
protected float bulletDuration;
protected string gunName;
//public GameObject bullet;
protected float shootTimer;
protected Animator anim;
protected Weapons(float damage, float speed, float shootDelay, float bulletDuration, string gunName)
{
this.damage = damage;
this.speed = speed;
this.shootDelay = shootDelay;
this.bulletDuration = bulletDuration;
this.gunName = gunName;
}
public abstract void Shoot(string shootDir);
}
Inherited class code including problematic Shoot method: public class Pistol : Weapons {
public GameObject bullet;
public List<Transform> shotSpawns;
public Pistol(float damage = 2f, float shootDelay = 0.5f, float speed = 15f, float duration = 0.6f, string name = "Pistol") :
base(damage, shootDelay, speed, duration, name)
{
print(gunName + " equipped");
}
void Awake()
{
shootTimer = Time.time;
anim = gameObject.GetComponent<Animator>();
if (bullet == null)
Debug.Log("No bullet object!");
}
void Start()
{
shotSpawns = new List<Transform>();
foreach (Transform t in GetComponentsInChildren<Transform>())
{
if (t.tag == "ShotSpawn")
shotSpawns.Add(t);
}
if (shotSpawns != null)
{
Debug.Log("Shot Spawns initialized!");
Debug.Log(shotSpawns.Count + " transforms added");
for (int i = 0; i < shotSpawns.Count; i++)
Debug.Log("Shotspawn " + i + " added: " + shotSpawns[i]);
}
else
Debug.Log("No transforms added!");
}
public override void Shoot(string shootDir)
{
if (Time.time < shootTimer)
return;
shootTimer = Time.time + shootDelay;
if (shotSpawns != null)
Debug.Log("Shotspawns in Shoot method!");
else
Debug.Log("ShotSpawns is NOT in shoot method!");
//anim.SetBool("Shooting", true);
switch (shootDir)
{
case "Right":
Instantiate(bullet, shotSpawns[0].transform.position, shotSpawns[0].transform.rotation);
break;
case "Left":
Instantiate(bullet, shotSpawns[1].transform.position, shotSpawns[1].transform.rotation);
break;
case "Up":
Instantiate(bullet, shotSpawns[2].transform.position, shotSpawns[2].transform.rotation);
break;
case "Down":
Instantiate(bullet, shotSpawns[3].transform.position, shotSpawns[3].transform.rotation);
break;
case "UpLeft":
Instantiate(bullet, shotSpawns[4].transform.position, shotSpawns[4].transform.rotation);
break;
case "DownLeft":
Instantiate(bullet, shotSpawns[5].transform.position, shotSpawns[5].transform.rotation);
break;
case "UpRight":
Instantiate(bullet, shotSpawns[6].transform.position, shotSpawns[6].transform.rotation);
break;
case "DownRight":
Instantiate(bullet, shotSpawns[7].transform.position, shotSpawns[7].transform.rotation);
break;
}
}
}
Answer by Bunny83 · Nov 08, 2017 at 02:47 AM
Where / how do you actually call your "Shoot" method? Are you sure you do not reference a prefab and trying to call "Shoot" of the prefab instance? Prefabs are not active objects. They won't have it's Start method called.
If it is actually attached to an object in the scene, are you sure the object is not deactivated and the script is actually enabled?
Finally depending on your event flow it would be also possible that you call Shoot before Start ran. However there are many unknown here that we can't answer.
ps: Your base class should be named "Weapon" and not "Weapons". The class represents one weapon and not a collection. This is quite irrelevant to the problem but it's just a naming thing which should be refactored.
Thanks for your input. The player object calls Shoot() on button down during Update. Pistol is attached to the player, as I'm just trying to get it to work and it is active.
Below is the relevant Player.cs script: public class Player : $$anonymous$$onoBehaviour {
public float moveSpeed = 2f;
public float shootDelay = .5f;
public GameObject bullet;
private string facing = "Down";
private float shootTimer;
private bool walking = false;
private int gunSelect = 0;
private Vector3 lastInput;
private Animator anim;
private Weapons gun;
private List<Weapons> weapons;
void Start()
{
shootTimer = Time.time;
anim = GetComponent<Animator>();
Pistol gun = new Pistol();
weapons = new List<Weapons>();
weapons.Add(gun);
}
void Update()
{
if (Input.GetAxisRaw("Horizontal") != 0f || Input.GetAxisRaw("Vertical") != 0f)
{
lastInput = GetInput();
anim.SetFloat("xDir", lastInput.x);
anim.SetFloat("yDir", lastInput.y);
transform.position += lastInput * moveSpeed * Time.deltaTime;
}
else
{
walking = false;
anim.SetBool("Walking", false);
}
if (Input.GetButtonDown("Fire1"))
{
string facing = Deter$$anonymous$$eDirection(lastInput);
weapons[gunSelect].Shoot(facing);
}
else
{
anim.SetBool("Shooting", false);
}
}
}
No, you haven't attached the Pistol to the player object. You created an instance with "new" which is not possible for $$anonymous$$onoBehaviour (or Components in general):
Pistol gun = new Pistol();
If you want to attach a script dynamically you have to use gameObject.AddComponent<Pistol>()
. However usually you would attach the required scripts in the inspector where you can configure them. In your Player script you can access your Pistol by using GetComponent<Pistol>()
. You can also use GetComponents<Weapon>()
which returns an array with all components attached to the player object that are derived from Weapon (assumed you renamed the class)
Thanks so much. I originally had it not attached to the object and was adding it dynamically. I'm just stating to try to incorporate inheritance in all my coding and appreciate your help. We got it working! Cheers.
Your answer
Follow this Question
Related Questions
Multiple PowerUps (One ItemBox) 0 Answers
Why is my Camera not rotating properly? 1 Answer
Can not access an abstract class from another script! 0 Answers
Opening and closing a Gate on Input.GetButtonDown 0 Answers
What is the most effective way to structure Card Effects in a Single Player game? 1 Answer