Jump to content

You're referencing something that does not exist. That is the cause.

 

What is actually happening can only be explained with an excerpt of the code.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
https://linustechtips.com/topic/800846-c-noob-needs-help/#findComment-10071865
Share on other sites

Link to post
Share on other sites

At line 14 you are referencing something that has "null" value.

If you look at the code below:

Enemy enemy;

if(something)
{
  enemy = new Enemy();
}

enemy.Update();  <-- HERE YOU CAN EXPECT AN ERROR IF CONDITION IS NOT MET BECAUSE ENEMY HAS NOT BEEN INSTANTIATED (IS NULL)

it should give you a rough idea of what to look for.

 

Since you get that error in "Enemy.Update()", I would imagine you are trying to call method Update() on a null object.

Try, fail, learn, repeat...

Link to comment
https://linustechtips.com/topic/800846-c-noob-needs-help/#findComment-10072019
Share on other sites

Link to post
Share on other sites

2 hours ago, zwirek2201 said:

At line 14 you are referencing something that has "null" value.

If you look at the code below:


Enemy enemy;

if(something)
{
  enemy = new Enemy();
}

enemy.Update();  <-- HERE YOU CAN EXPECT AN ERROR IF CONDITION IS NOT MET BECAUSE ENEMY HAS NOT BEEN INSTANTIATED (IS NULL)

it should give you a rough idea of what to look for.

 

Since you get that error in "Enemy.Update()", I would imagine you are trying to call method Update() on a null object.

The problem is that "Enemy" is definitely instantiated

Link to comment
https://linustechtips.com/topic/800846-c-noob-needs-help/#findComment-10072770
Share on other sites

Link to post
Share on other sites

9 minutes ago, AleksanderK said:

Here's my script, by the way

 

Well, this is not your script, it's just the code that creates class Enemy, you would have to post the snippet of code where you actually use the class and where you instantiate it. Then we would be able to help you.
 

Try, fail, learn, repeat...

Link to comment
https://linustechtips.com/topic/800846-c-noob-needs-help/#findComment-10072815
Share on other sites

Link to post
Share on other sites

3 minutes ago, zwirek2201 said:

Well, this is not your script, it's just the code that creates class Enemy, you would have to post the snippet of code where you actually use the class and where you instantiate it. Then we would be able to help you.
 

But doesn't the object's presence in the scene mean it's instantiated?

Link to comment
https://linustechtips.com/topic/800846-c-noob-needs-help/#findComment-10072829
Share on other sites

Link to post
Share on other sites

3 minutes ago, Yamoto42 said:

Reading your original post...

 

Object reference not set to instance of object.

Object...not...instance.

Something, somewhere, that you think exists does not.

Well, that would be very strange, because the only objects referenced by this script are the waypoints and the enemy. All of them exist in this scene

 

Link to comment
https://linustechtips.com/topic/800846-c-noob-needs-help/#findComment-10072851
Share on other sites

Link to post
Share on other sites

Don't have any experience with unity, but from what I'm getting you need to initialize an instance of the class "enemy"

A class is not a variable nor an object, it is rather a blueprint for an object, containing all of the functions and variables that define the object.

Link to comment
https://linustechtips.com/topic/800846-c-noob-needs-help/#findComment-10100393
Share on other sites

Link to post
Share on other sites

On 6/30/2017 at 1:01 PM, AleksanderK said:

Well, that would be very strange, because the only objects referenced by this script are the waypoints and the enemy. All of them exist in this scene

 

It's not strange, you're attempting to use a object/attribute that doesn't have a value or isn't instantiated.  It may be that your enemy was no longer in scope and you referenced it, it could be a specific attribute that you're trying to use that hasn't been set, it could be a function call that is supposed to return a list and doesn't, or it could be any number of other things. 

 

What we know for certain is that you're attempting to use something that doesn't exist, period.  Argue about what the environment says all you want, that's what the error means and if you knew even a tiny bit about coding you'd realize that.  Stop making games and take a CS101 course and learn to code first, copying code and scripting isn't going to help you when you don't have the bare bones fundamentals.

 

edit: also... I don't use Unity because I'd rather have open source, but if I had to hazard a guess you've put them on the scene but you haven't actually spawned the enemy and you're trying to do something to manipulate it before it's actually created.

Link to comment
https://linustechtips.com/topic/800846-c-noob-needs-help/#findComment-10100418
Share on other sites

Link to post
Share on other sites

Looking at the error it is referencing line 14 in Enemy.cs, which seems to be the one you have screenshottet.

 

If we look at that line you are trying to get the property 'position' of the objects 'target' and 'transform'. Since you are extending Monobehavior I believe the 'transform' object will always be instantiated.

 

But 'target' is a private property, and from the code you are sharing it doesn't look like you've instantiated it. If I remember correctly you can instantiate a public property that is an object by 'drag'n'drop' in Unity. But 'enemy' is not public. 

 

So I would guess it throws an exception because 'target' is not an instance of an object. I cannot be sure without looking at the entire project though.

Link to comment
https://linustechtips.com/topic/800846-c-noob-needs-help/#findComment-10102199
Share on other sites

Link to post
Share on other sites

Look at speed and wavepointIndex for a moment, they have been instantiated because of "= <value>;" beside them.

Your Transform property does not have that, therefor it has not been given a value.

CPU - Ryzen 7 3700X | RAM - 64 GB DDR4 3200MHz | GPU - Nvidia GTX 1660 ti | MOBO -  MSI B550 Gaming Plus

Link to comment
https://linustechtips.com/topic/800846-c-noob-needs-help/#findComment-10103016
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×