So, I have run into a couple problems. Problem 1 is in my EnemyPathing.cs (pasted below) that keeps popping up, and has cuased me to have to rewrite the entire code at least once, i dont want to do that again…
I keep getting a null exception error when, 1. I add an enemy prefab to the scene. If I don’t have the prefab in the scene, all enemies spawn, move through their intended paths and destroy them selves at the end of the line, cycling through multiple waves, all as intended.
But place a static Prefab enemy in the scene to test the damage, and it breaks the game.
2. Projectile collision. When the game is cycling through the enemie waves, all seems to be working correctly if an enemy prefab is not on the screen, but when I try to shoot one the moving enemies, I get a different Null Exception, from the Enemy Class…
I know a Null Exception means a the Object Reference is not set to an instance of the object… but I have followed all the instructions, attached all the scripts to my prefabs… I dont understand…
Enemy Pathing -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyPathing : MonoBehaviour
{
List waypoints;
WaveConfig waveConfig;
int waypointIndex = 0;
float moveSpeed;
// Start is called before the first frame update
void Start()
{
waypoints = waveConfig.GetWaypoints();
transform.position = waypoints[waypointIndex].transform.position;
moveSpeed = waveConfig.GetMoveSpeed();
}
// Update is called once per frame
void Update()
{
Move();
}
public void SetWaveConfig(WaveConfig waveConfig)
{
this.waveConfig = waveConfig;
}
private void Move()
{
**if (waypointIndex <= waypoints.Count - 1)** //(this is where double clicking the pathing error leads me)
{
var targetPos = waypoints[waypointIndex].transform.position;
var movementThisFrame = moveSpeed * Time.deltaTime;
transform.position = Vector2.MoveTowards(transform.position, targetPos, movementThisFrame);
if (transform.position == targetPos)
{
waypointIndex++;
}
}
else
{
Destroy(gameObject);
}
}
}
Enemy -
public class Enemy : MonoBehaviour
{
[SerializeField] float health = 100;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D other)
{
DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
**health -= damageDealer.GetDamage();** // this is where double clicking the Collider Error leads me.
}
}
Damage Dealer -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DamageDealer : MonoBehaviour
{
[Range(1, 500)][SerializeField] int damage = 100;
public int GetDamage() { return damage; }
public void Hit()
{
Destroy(gameObject);
}
}