Please help

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);
}

}

Hi,

NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see to which line in your code it is referring. If you exposed a field in the Inspector, make sure that it’s not empty.


See also:

Both errors take me to the scripts ( I actually indicated where Double clicking leads, with comments in the script above.) I have doubled checked the inspector multiple times… This is why I am lost…

error1

these are the actual error that are showing in the console.

Does the “other” game object have got a DamageDealer component attached?

Have you already tried to add Debug.Logs to your code to see what is going on with waypoints during runtime?

No, I have not… Im a complete newb, have limited experience, and not 100% sure how to implement that, but I will attempt it.
As for the “other” game object… Im not sure, I guess. I thought so. I will triple check, when I am able to continue troubleshooting.

Ok, so… The projectile prefab was missing the Damage Dealer Script, I had not applied the overide correctly, when attaching the script.
However, I still cannot figure out what is happening on my static Enemy Prefab. I was able to slow the moving enemies enough to test that their health is reducing when the projectile trigger collides, but that is only on the enemies spawned by the waveConfigs.
I did attempt to Debug.Log the Move function… but I can not seem to gleam any information, so mostlike implemented this incorrectly…

if (waypointIndex <= waypoints.Count - 1)
        {
             var targetPos = waypoints[waypointIndex].transform.position;
             var movementThisFrame = moveSpeed * Time.deltaTime;
             transform.position = Vector2.MoveTowards(transform.position, targetPos, movementThisFrame);
            Debug.Log("Im attmepting to Debug the way point list" + targetPos);
            if (transform.position == targetPos)
                 {
                waypointIndex++;
                 }

        }
        else
        {
            Destroy(gameObject);
        }

I removed the Enemy Pathing Script from the Instanced Static Enemy, and that fixed the problem, and allowed me to test and ensure that the damage was correctly calculating, but I would still like to understand why I was getting the pathing error.
How should I implement the Debug.Log to correctly assess the information?

If you get a NullReferenceException, you ideally check the value of your variables during runtime, e.g. via Debug.Log("Value of myVariable: " + myVariable);. Once you found the variable which is null, you check where an object was supposed to get assigned to it. And you do that until you found where the object did not get assigned to the/a variable. That’s all.

Debugging looks harder than it is. You basically do the same over and over again: checking and interpreting values.


See also:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms