I’m trying to inherit from an Enemy class to a Guard class but NOTHING IS INHERITING! How does this all work??? I’ve been at this for hours. I just keep coming up with UnsignedReferenceExceptions and things aren’t working. Here’s my code:
// Enemy.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Enemy : MonoBehaviour
{
protected NavMeshAgent navMeshAgent = null;
public List<GameObject> pathNodes = new List<GameObject>();
protected int currentPathNodeIndex = 0;
public float pathNodeDistanceThreshold = 1.0f;
public GameObject player = null;
public float aggroRadius = 5.0f;
public int delayRange = 0;
public float timer = 0.0f;
// Use this for initialization
void Start ()
{
delayRange = Random.Range(2, 10);
navMeshAgent = gameObject.GetComponent<NavMeshAgent>();
if (navMeshAgent == null)
{
navMeshAgent = gameObject.AddComponent<NavMeshAgent>();
}
if (pathNodes.Count <= 0)
{
Debug.LogError("This sytem requires that at least one path node blah blah blah....");
}
player = GameObject.FindWithTag("Player");
}
// Update is called once per frame
void Update ()
{
if (player == null)
{
return;
}
if (navMeshAgent == null)
{
return;
}
if (pathNodes.Count <= 0)
{
return;
}
}
}
// Guard.cs
// Here, Guard isn't inheriting the Player so he can't find him
//Why? Shouldn't the Enemy grab the object with the tag, "Player" and therein allowing Guard to use it?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Guard : Enemy
{
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Vector3.Distance(gameObject.transform.position, player.transform.position) <= aggroRadius)
{
navMeshAgent.destination = player.transform.position;
}
else
{
navMeshAgent.destination = pathNodes[currentPathNodeIndex].transform.position;
if (Vector3.Distance(gameObject.transform.position, navMeshAgent.destination) <= pathNodeDistanceThreshold)
{
timer = timer + Time.deltaTime;
if (timer >= delayRange)
{
timer = 0.0f;
currentPathNodeIndex++;
delayRange = Random.Range(2, 10);
}
if (currentPathNodeIndex >= pathNodes.Count)
{
currentPathNodeIndex = 0;
}
}
}
}
}
Why isn’t this working? Also, how do I put prefabs into lists at runtime? I need to do that so I can create new PathNodes and Enemy is able to grab those nodes and put them in the list. Any help would be appreciated. Inheritance just confuses me greatly…