Search Unity

HEX RTS strategy | Solved | The problem with touching the rays of the mesh

Discussion in 'Scripting' started by serega1103, Aug 1, 2019.

  1. serega1103

    serega1103

    Joined:
    Aug 1, 2019
    Posts:
    3
    Hello!

    I am writing a HEX RTS strategy for this lesson - https://catlikecoding.com/unity/tutorials/hex-map/part-1/, doing the same thing. Camera emission does not work.

    An error pops up in the console:
    NullReferenceException: Object reference not set to an instance of an object
    on this line:
    Code (CSharp):
    1. Ray inputRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    At the same time, I added a collider to the grid, in Awake (), so that there was something to face:
    Code (CSharp):
    1. meshCollider = gameObject.AddComponent <MeshCollider> ();
    And at the end of the method that deals with triangulation, made an assignment:
    Code (CSharp):
    1. // after the triangulation is complete, assign a mesh to the collider
    2. meshCollider.sharedMesh = hexMesh;
    Tell me what could be the problem?

    Project: https://drive.google.com/file/d/1kuy009sQ0sQYHkAsaMg_BYPqzqADoCeU/view?usp=sharing

     
  2. PanicEnsues

    PanicEnsues

    Joined:
    Jul 17, 2014
    Posts:
    187
    What is on line 40 of HexMesh.cs?
     
    serega1103 likes this.
  3. serega1103

    serega1103

    Joined:
    Aug 1, 2019
    Posts:
    3
    Code (CSharp):
    1. Ray inputRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    If anything, the project can be downloaded from Google Drive using the link in the post.
     
  4. serega1103

    serega1103

    Joined:
    Aug 1, 2019
    Posts:
    3
    OMG, solved. Thanks to PanicEnsues and everyone.

    I spent half a day on this.
    I have one camera in the project - the main one.
    I checked that the script is assigned to the desired object.
    I explicitly assigned the main camera to a variable.

    This did not help.

    To identify the camera, I needed to write this:
    (GameObject.Find ("Main Camera"). GetComponent <Camera> ()).

    Final code:

    Code (CSharp):
    1.  
    2.     void Update()
    3.     {
    4.         // handle when the mouse button is held down
    5.         if (Input.GetMouseButton(0))
    6.         {
    7.             HandleInput();
    8.         }
    9.     }
    10.  
    11.     // executed when the mouse button is held down
    12.     void HandleInput()
    13.     {
    14.         // structure that contains information about the hit and the point of contact
    15.         RaycastHit hit;
    16.         // throw the beam from the camera onto the stage
    17.         Ray inputRay = (GameObject.Find("Main Camera").GetComponent<Camera>()).ScreenPointToRay(Input.mousePosition);
    18.         // if the beam collides with something, extract the MeshDeformer component from the object that was hit
    19.         if (Physics.Raycast(inputRay, out hit))
    20.         {
    21.             TouchCell(hit.point);
    22.         }
    23.     }
     
  5. PanicEnsues

    PanicEnsues

    Joined:
    Jul 17, 2014
    Posts:
    187
    Great, glad you found it!

    For performance purposes, you might want to make the camera a public variable (e.g. "m_camera") that you can drag/drop your camera onto, then just call it with
    Ray inputRay = m_camera.ScreenPointToRay(Input.mousePosition);

    -Scott
     
    serega1103 likes this.