Game object error

Please Help
I’m getting an " Object reference not set to an instance of an object" error and I can’t figure out why :0
I went through the course content and still couldn’t see where I have gone wrong. our code is identical as far as I can tell

Code as follows, Getting the error on the first line of my update method.

    private void Update() 
    {
        bool isInRange = Vector3.Distance(transform.position, target.position) < weaponRange;

        if (target != null && !isInRange)
        {
            GetComponent<Mover>().MoveTo(target.position);
        }
        else
        {
            GetComponent<Mover>().Stop();
        }
    }

Okay so the error line is indicating the boolean.
Can you post a screenshot of the player in unity and see how that is set up

Gotcha,

Line 15 is the reference to GetComponent().MoveTo(target.position)
If you look at the screenshot you havent got a target in Mover script.

If i remember at this point you need to drag the target from the scene into the Mover script in the inspector.

If this was the issue i have to warn you that you might want to run the basic courses again as the saving section will give you a lot of trouble. (I had to work to learn some of it and i played with mudcode doing exactly the same thing to playerfiles)
I dont say this to be mean as sometimes i forget to drag things in the inspector, it happens but i just wanted you to have all the information at this point in time :slight_smile:

Let me know if this isnt the issue and we can look further into it.

Thanks

I took a screenshot of the lecture at the same point i should be at and its empty there as well. the target should be set by our raycast when we click.

I have already started the beginner course again, thanks for the advice tho :slight_smile:

@ben
Any ideas on this one mate? I’m would love to continue with the course but am stuck here atm.

I just copied the code from the GitHub Repository and am still getting the same problem.

the target should be set by our raycast when we click.

So, the first thing to check is is this actually happening? The word “should” is always a bit of a flag that you should dig a bit deeper and be certain this is doing what you expect.

Secondly, even if target is given a value when you click, Update() is trying to access target.position every frame, right from the moment the game starts, in order to determine isInRange. So, unless target has a value assigned to it on Start(), you’ll get an error here every frame until your first click, because it can’t access the position property of nothing.

Hope this helps!

~ Jonny

From my understanding, we are raycasting every update and taking note of weather it hits an enemy, a walk-able surface, or off the map. so that target is always the vector3 of the mouse position, and clicking only activates the method that moves the player over there.

I just downloaded the entire project at this point from Github to try and see what i have different, but it has the same problem. Really not sure where to go from here.

@Marc_Carlyon

OMG -_-

I finally gave up and tried to just continue with the lectures in the hope that it would fix itself and the next lecture literally fixed it…

I was getting caught up because my Unity was pausing the game and not letting me do anything, but it wasn’t pausing in the lecture… Thanks for responding and trying to help

1 Like

Glad you were able to figure out a solution and that the next lecture fixed it!

(You can stop Unity pausing the game when you get an error by making sure ‘Error Pause’ in the console window is de-selected :slight_smile: )
image

1 Like

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

Privacy & Terms