Hello!

I'm new to Unity . I make a 2D game. When trying to track where to click on the "playing field" I got the following error in the console:

NullReferenceException: Object Reference. Select.Update () (at Assets / Scripts / Select.cs: 23)

The method in which this occurs:

void Update() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast (ray, out hit, 100)) { Debug.Log ("Hit something!"); } else { Debug.Log ("No Hit something!"); } } 

23 line:

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 

Can you set me on the right path? And I will be grateful if you advise literature for development in scripting on Unity .

  • On the official website of Unity there is a lot of information on development in this environment, including programming. Perhaps you have not seen all the sections? On literature specifically on Unity, where something would be written about the development, fundamentally better than the official site did not come across. - BadIrbis

1 answer 1

Most likely you have changed the camera tag. Suppose you change it to Select . Meanwhile, Camera.main looking for a camera with the MainCamera tag.

That is, as a solution: you need to either change the tag back to the desired one, or search for the camera by tag:

 Camera cam = GameObject.FindWithTag("YOUR_TAG").GetComponent<Camera>(); Ray ray = cam.ScreenPointToRay(Input.mousePosition); 

or through Find , like a normal gameObject (it works slower):

 GameObject.Find("CAMERA_NAME").camera Ray ray = cam.ScreenPointToRay(Input.mousePosition); 

or some other way in which you can find the desired object.

PS Usually, Ray and MousePosition are used when you press the mouse button, that is, they usually write something like:

 if (Input.GetButtonDown("Fire1")) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray)) { // do smth } } 

This is in case you suddenly planned just such a logic. That in a single beam not to let.

  • I need to select objects using a ray (change the value of a boolean variable). Tell me which object you need to hang the script? - Giggs
  • @Giggs it all depends ..... maybe you have a player and he shoots someone from a gun, then the beam should come from him. Or maybe you just have a camera and moving mobs and you poke it with a mouse, then you can go to the camera. Everything rests on logic, on what is and what is planned - Alexey Shimansky
  • Thank you for your help. - Giggs
  • There was another problem. if(Input.GetMouseButtonDown(0)){ if (Input.GetKey (KeyCode.LeftShift)) { Ray selectRay = Camera.main.ScreenPointToRay (Input.mousePosition); if (Physics.Raycast (selectRay)) { Debug.Log (this.gameObject.tag); } } } if(Input.GetMouseButtonDown(0)){ if (Input.GetKey (KeyCode.LeftShift)) { Ray selectRay = Camera.main.ScreenPointToRay (Input.mousePosition); if (Physics.Raycast (selectRay)) { Debug.Log (this.gameObject.tag); } } } to the console that the camera is marked and not another object - Giggs
  • @Giggs do raycast as you did in your example Physics.Raycast(selectRay, out hit, 100) ... and then take the tag from hit.gameObject.tag - Alexey Shimansky