0

So when I am clicking on the object in the game I get this error...

NullReferenceExceptionm : Object reference not set to an instance of an object JumpDestination.Update () (at Assets/Scripts/JumpDestination.cs.:12)

I don't know what I am doing wrong,how can I fix it? I want to get the position of the hited object.

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

public class JumpDestination : MonoBehaviour {

    private RaycastHit hit;
    public float jumpMaxDistance;

    void Update(){
        Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hit, jumpMaxDistance);
        if (hit.collider.gameObject.tag == "RichPoint") {
            print (hit.collider.transform.position);
        }
    }
}
Dinu Adrian
  • 129
  • 2
  • 13
  • Which line of code is causing that error? Double click on the error from Unity's Editor and it will take you to that line. – Programmer Mar 19 '17 at 01:23
  • You should check the result of `Physics.Raycast` before trying to get `hit`. Check that `hit` is not null. Then check that `hit.collider` is not null. Then check that `hit.collider.gameObject` is not null. – jhh Mar 19 '17 at 01:29

1 Answers1

1

I don't know what I am doing wrong,how can I fix it? I want to get the position of the hited object.

3 things you did wrong:

1.You did not check if mouse is pressed before raycasting.

2.You did not check if Physics.Raycast hit anything before printing the object's position.

3.You defined the hit variable outside a function. Not a good idea because it will still store the old object the mouse hit. Declare that in the update function.

FIX:

void Update()
{
    //Check if mouse is clicked
    if (Input.GetMouseButtonDown(0))
    {
        RaycastHit hit;

        //Get ray from mouse postion
        Ray rayCast = Camera.main.ScreenPointToRay(Input.mousePosition);

        //Raycast and check if any object is hit
        if (Physics.Raycast(rayCast, out hit, jumpMaxDistance))
        {
            //Check which tag is hit
            if (hit.collider.CompareTag("RichPoint"))
            {
                print(hit.collider.transform.position);
            }
        }
    }
}

Regardless, this answer was made to show you what you did wrong. You should not be using this. Use Unity's new EventSystems for this. Check the 5.For 3D Object (Mesh Renderer/any 3D Collider) from this answer for proper way to detect clicked object.

Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • IT WORKED! Man....You saved me! THANK YOU SO MUCH!.I have a little question tho. how much time did it take for you te get good at programming...I started like a month ago,i am just curious,just wanna know for my motivation . – Dinu Adrian Mar 19 '17 at 01:58
  • Some say 2 years, 6 months or so. The truth is that you get good at programming depending on how much time you spend programming. Do that everyday. Learn something new and make friends with people that can program.Learn from them too. Work on a random project weekly. This is up to you. If you are in school ask your professor how to improve your code for every project/homework you do. If you do it everyday, 5 months should be fine. – Programmer Mar 19 '17 at 02:05
  • 1
    Good advice.Thank you! – Dinu Adrian Mar 19 '17 at 02:11