NullReferenceException: Object reference not set to an instance of an object

Hello again! tis’ me! Darkness! I keep running into these errors that values are unassigned even tho the videos do fine without assigning any values to target which is supposed to be assigned when the enemy appears and is in range. I had this issue earlier when i was supposed to set the coordinates of my tiles where i had to safeguard it with a null. I wonder if im doing something wrong and why this is working in the videos when it doesnt work for me. Any assistance is greatly appreciated! <3

You’re calling AimWeapon() before FindTarget(). If target is unassigned, you’ll get an error on line 41 when calling target.position.

I would suggest calling FindTarget() first (which logically makes sense – we wouldn’t aim our weapon before deciding who to aim at!).

Then I would check whether target is null at the start of AimWeapon(). If targetDistance >= maxDistance (line 29), you will be setting target to null on line 36. Since there are cases where you are assigning null to target, it would be a good idea to check for this.

Many thanks for the quick reply! :smiley: I will try this out right away

Hey, so I’ve tried implementing it and whenever there are no enemies on the map and there is no target it freezes. How would I go about to safeguard for this? I’m thinking i need to set an idle state for if there are no enemies. Maybe I’m getting ahead of the course content. Thanks for your help once again :slight_smile:

What’s freezing? Are you able to post your updated code?


I have the same code (unless ive missed something really obvious), the only thing i can think of is that the script in the editor does not have anything assigned to it but when i press on the prefab it has a weapon assigned to it. I can however set a null to attack itself and it works but then the range doesnt work. I have no clue :thinking:

Try assigning the weapon in the inspector. :slight_smile:

If weapon is null then you have nothing to call LookAt() on.

no what i mean is that
image
isn’t set and


is :thinking: which uhh is confusing. What could be causing this? been stuck on it for a few hours now :smiley:

Yes – you haven’t assigned the weapon. You can either drag and drop your weapon in, or click the circle next to None (Transform) and select your weapon there.

Haha sorry for my ignorance but its grayed out and when I click it it says None :smile:

I mean I can’t drag my weapon or my particle system on it, it won’t allow it (only on the prefabs, which is what I’ve done) but it works until there are no enemies alive. which is when it freezes and says not set to an object. so i think its the lack of a target? :confused:

As I said before, I’d check whether target is null at the start of AimWeapon(). :slight_smile:

When there are no enemies, closestTarget is still null when you are setting target = closestTarget.

Ahhh yeah, could you be more specific on this condition?
like
if (target == null)
{
target = what to make it work
}
I’m not sure what to even make this because before I just set it to transform which made it attack itself. How can I make it pause or something or idle. Seems to work in the videos without checking for a null, but I don’t know he’s always had enemies on the screen :smile: i get that maybe i shouldnt ask you to write the code for me but a hint would be appreciated

At the start of AimWeapon():

if (target == null) return;

(Making some presumptions about the intended functionality as I’m not enrolled on this course) You only want the code in this method to run when there is a target. If there is no target, we just return from (exit) the method. If there is a target, we continue to check the distance and attack.

There are a few different ways to do this. An alternative would be in Update():
if (target != null) AimWeapon();

This way, we only call AimWeapon() when there is a target.

Hope that does what you need, good luck. :slight_smile:

1 Like

I will give that a go, thanks a bunch for your, patience, help and time! :smiley:

1 Like

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

Privacy & Terms