Lost in Laser Defender


So basically I have no idea what’s going on.

Hi,

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.

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.

Hope this helps :slight_smile:


See also;

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

public class EnemyPathing : MonoBehaviour
{

WaveConfig waveConfig;
List<Transform> waypoints;
int waypointIndex = 0;

// Use this for initialization
void Start()
{
    waypoints = waveConfig.GetWaypoints();
    transform.position = waypoints[waypointIndex].transform.position;
}

// 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)
    {
        var targetPosition = waypoints[waypointIndex].transform.position;
        var movementThisFrame = waveConfig.GetMoveSpeed() * Time.deltaTime;
        transform.position = Vector2.MoveTowards
            (transform.position, targetPosition, movementThisFrame);

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

}

NullReferenceException: Object reference not set to an instance of an object
EnemyPathing.Move () (at Assets/Scripts/EnemyPathing.cs:32)
EnemyPathing.Update () (at Assets/Scripts/EnemyPathing.cs:22)

This is Rick’s Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyPathing : MonoBehaviour {

WaveConfig waveConfig;
List<Transform> waypoints;
int waypointIndex = 0;

// Use this for initialization
void Start () {
    waypoints = waveConfig.GetWaypoints();
    transform.position = waypoints[waypointIndex].transform.position;
}

// 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)
    {
        var targetPosition = waypoints[waypointIndex].transform.position;
        var movementThisFrame = waveConfig.GetMoveSpeed() * Time.deltaTime;
        transform.position = Vector2.MoveTowards
            (transform.position, targetPosition, movementThisFrame);

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

}

I think this is what you wanted. I thought that there was something not linked in the inspector but IDK how to tell what it is.

Double click on the error message. To which line in your code does it refer?

EnemyPathing.cs
Lines 15 & 32.

These are lines 15 and 32 for me:

}

There is no variable which could be null.

Line 15:
waypoints = waveConfig.GetWaypoints();
Line 32:
if (waypointIndex <= waypoints.Count - 1)
I assume I should google “there is no variable which could be null” because I don’t know what that means.

Look like it cant find the waypoints, null means; no value, nothing.
Did you drag in the waveConfig scriptable object?
and the path prefab into that?

When I add Waveconfig to the “Enemy Wave Config” Object Path 1 I can’t edit it in the inspector.
When I make a new “Enemy Wave Config” Obj Path 2 I can’t add the script.
In the Path 2 I can edit enemy prefab, path, #, & move speed.
Same Error EnemyPathing.CS line 32:
if (waypointIndex <= waypoints.Count - 1)
Same Error Line 15:
waypoints = waveConfig.GetWaypoints();

When I try to add the WaveConfig.cs to object I get :
“The script don’t inherit a native class that can manage a script.”

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? Does the file name match the class name inside the file? For example, is the WaveConfig class inside the WaveConfig.cs file?

If the problem persists, please watch lecture “Fixing Visual Studio Problems” (currently #4).

Thank you very much I appreciate your assistance.
I’ve compared my script to the videos in 90-98 & I also downloaded Rick’s scripts off github. I followed the class & that’s where the errors began. When I tried to copy/paste Rick’s code I still got errors. I watched video 4 as you told me & did everything it said to do. I really don’t know what video I was up to when this error began.
After watching these videos again & again I saw something in WaveConfig.cs that was not like it was in Rick’s video. After changing that & testing unity I got the following errors:
No script asset for WaveConfig. Check that the definition is in a file of the same name and that it compiles properly.
Assets\Scripts\EnemyPathing.cs(15,32): error CS1061: ‘WaveConfig’ does not contain a definition for ‘GetWaypoints’ and no accessible extension method ‘GetWaypoints’ accepting a first argument of type ‘WaveConfig’ could be found (are you missing a using directive or an assembly reference?)
------------------You have line 15, this is line 32:
if (waypointIndex <= waypoints.Count - 1)

The spelling is correct everywhere, isn’t it? The WaveConfig class contains a GetWaypoints method, doesn’t it?

If so, check your Assets folder. There must not be any duplicates of scripts. Two WaveConfig files might cause a conflict.

If you ensured that there is only once class/script, click the right mouse button in the Assets folder, then on “Reimport all”.

If that didn’t fix the problem, close Unity and Visual Studio. Then open your project folder and delete the Library folder and the Temp folder (if there is such a folder), and the .sln and .csproj files. Start Unity again and open your project. This might take a few minutes because Unity will recreate the Library folder.

1 Like

Thank you. It looks like my issue is lesson 95. Create list of waypoints at 8mins (8:07 to be exact). When I click on my enemy prefab I see the enemy pathing script I don’t have the waypoints option in my inspector like Rick does.
It would be impossible (unless Rick mispelled it in his code) for me to have incorrect spellings because I have copy & pasted my code from Rick’s code but I did check the spelling.
I did what you told me to & these were my results:
1- I get the same errors on 15 & 32 of enemy pathing
2- when I click on the enemy prefab I have no parameters in the inspector. My only option is to open prefab & when I do so I can’t do anything but look at the prefab enemy ship. In the inspector it just says open prefab when I click to open the prefab (& the prefab edit window is open). Even when I click again open prefab nothing happens. So I can do nothing with this prefab (enemy) now.
“Prefab has missing scripts. Open prefab to fix the issue.”
3- My prefab player has the same issue. These were not issues before I “Reimport all” but now they are.

Open the prefab. Then remove the “missing” scripts and readd them. Sometimes, the Inspector is buggy for no apparent reason.

By the way, which version of Unity do you use?

When I do that I get.
The script don’t inherit a native class that can manage a script.
Unity 2020.3
Updated Visual Studios a few days ago.

Make a backup of your project folder by duplicating it. Save the duplicated folder somewhere else, maybe on an USB stick. To save some free space, you could delete the Library and Temp folders in the backup folder.

Then update Unity. Maybe you are experiencing a bug.

I’m fully updated now.
I did what you told me to do & I think you know the outcome.
“The script don’t inherit a native class that can manage a script."
Same Errors in line 15 & 32.
So I think this may help you; I’m taking another unity course & I get the same problem. Not only do I get “The script don’t…” I get the problem with errors even if I copy/paste scripts. What I means is there seams to be a problem with me adding new scripts to game objects, prefabs & sometimes anything. It’s hard for me to believe I’m having the same exact issue coincidentally although it can be.

Go to Window > Package Manager. Then check if there is a Visual Studio Editor package. If there isn’t, install it. If there is an update, install it.

Did you try the following?

If that didn’t fix the problem, close Unity and Visual Studio. Then open your project folder and delete the Library folder and the Temp folder (if there is such a folder), and the .sln and .csproj files. Start Unity again and open your project. This might take a few minutes because Unity will recreate the Library folder.

Maybe you’ll have to try that a second time. Then reboot your computer before launching Unity again.

Also check the console of Visual Studio. Maybe there is a message which does not pop up in Unity’s console.

Updated Visual Studios a few days ago.

Do you remember whether the problem occured before or after that update?

I greatly appreciate your help ma’am I’m sure you already know what I’m going to tell you.
I did everything you told me to do & nothing has changed.
The problem started before the update & still exists. It started with me trying to follow Rick’s code & then getting errors. Then I tried to solve the issue, even copy/pasted Rick’s code & you can fill in the blanks.
I do greatly appreciate you & the community. My issue is what do I have to do to finally get past this point? How many more days will we keep trouble shooting until we find, well hopefully find a solution…

Do you use an antivirus program or a firewall that might be messing around with Unity and/or VS?

If nothing helps, create a new project for testing purposes, then two scripts and copy and paste the code from Rick’s GitHub repository. If the same issue occurs, the problem is somewhere in Unity and/or VS, not in your project.

If you do not get the problem again, delete the test project and create a new project. Then copy and paste your scripts into the new Assets folder but no meta files. Open the new project in Unity. Then create a scene and try to connect the two scripts. Do not waste your time and make anything beautiful. If the scripts work as intended, make a backup of your entire new project folder. Then copy and paste the files you need from your old project.