Getting a Null Reference Exception Error - nothing works!

So I ran into some trouble a few videos ago because I’m using a more recent version of Unity. I was unable to drag the children of Path (0) to my Enemy Prefab. Looking at the Lecture Discussions for that video I found a discussion that addressed the exact issue. The gist of that discussion was don’t worry, this all gets resolved in a few videos when the Foreach Loop is implemented.

Well I’ve followed along and I believe that I have everything set up correctly but now I’m getting a NullReferenceException error.

Here’s a screenshot of that error.
20%20AM

So these errors tell me that “Object reference not set to an instance of an object… then the Method… in the EnemyPathing.cs script.”

I think I’ve got EnemyPathing set up exactly as Rick does. The only difference is I’ve changed the name of waypointIndex to waypointsIndex.

Here’s a screenshot of that.

I’m not sure where to go next. Any assistance would be greatly appreciated.

1 Like

I figured this out. I had to assign my enemy prefab to my wave. Duh. But maybe I’m actually learning!

Next problem, the enemy moves to my first waypoint but goes no further. More debugging and trouble shooting.

Any ideas that could point me in the right direction would be appreciated.

Thanks!

1 Like

Check the value for the Z axis on the waypoints transform position. A lot of students inadvertently set this to something other than zero, the enemies then look like they are not moving, but they are just stuck as they are constrained to only move on the X and Y and thus can never make it to the position on the Z axis.

Hope this helps and well done for resolving your other issue. :slight_smile:

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Hope this helps :slight_smile:


See also;

Thanks so much Rob,

Extremely helpful as usual!

Also, great tip on copying and pasting the code. Seems kinda silly to post a screenshot of code now that I actually think about it. :thinking: :slight_smile:

Thanks again!

Brian

1 Like

You’re very welcome Brian, happy to help, did that resolve the issue by the way?

I think some of the courses suggest posting a screenshot of your game and so on, so unfortunately I think it stems from the course content itself. Just easier as text for, well, everything :slight_smile:

All the best :slight_smile:

I was convinced that z axis was going to solve the problem but it didn’t. My waypoints z axis were all set to 0 but my enemy z axis was set to -1. I changed it to 0 with high hopes but no luck. I’ll have to review the videos to make sure I’ve got it all set up properly.

Trying out the code fencing with EnemyPathing.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyPathing : MonoBehaviour
{
    [SerializeField] WaveConfig waveConfig;
    List<Transform> waypoints;
    [SerializeField] float moveSpeed = 2f;
    int waypointsIndex = 0;

    // Start is called before the first frame update
    void Start()
    {
        waypoints = waveConfig.GetWaypoints();
        transform.position = waypoints[waypointsIndex].transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        Move();
    }

    private void Move()
    {
        Debug.Log(waypointsIndex);
        if (waypointsIndex <= waypoints.Count - 1)
        {
            Debug.Log(waypointsIndex);
            var targetPosition = waypoints[waypointsIndex].transform.position;
            Debug.Log(targetPosition);
            var movementThisFrame = moveSpeed * Time.deltaTime;
            transform.position = Vector2.MoveTowards
                (transform.position, targetPosition, movementThisFrame);

            if (transform.position == targetPosition)
            {
                waypointsIndex++;
            }
        }
        else
        {
            Destroy(gameObject);
        }
    }
}

If after you’ve had a look you’re still stuck share the project files with me here and I’ll happily take a quick look for you.

The forum will allow uploads of up to 10MB, if your project files (zipped) are larger than that you would need to use a service such as Google Drive or Dropbox, and then share the URL.

Privacy & Terms