Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Changing properties of object hit with raycast

Discussion in 'Scripting' started by realdka13, Jul 3, 2018.

  1. realdka13

    realdka13

    Joined:
    Jun 19, 2018
    Posts:
    23
    Im working on having an object change color and its collision box be disabled when the player clicks on it, it works, my issue is that it wont change back as soon as the player stops pressing it.
    It comes back with a null reference on this line :
    hit.collider.gameObject.GetComponent<MeshRenderer>().material.color = new Color32(255, 0, 0, 255);
    .
    I understand that hit is no longer assigned a value but why? And how do i fix it

    Code (CSharp):
    1.     public void RayCasting()
    2.     {
    3.         RaycastHit hit = new RaycastHit();
    4.  
    5.         Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    6.         Debug.DrawRay(ray.origin, ray.direction * 30, Color.green);
    7.         if (Input.GetMouseButtonDown(0))
    8.         {
    9.             if (Physics.Raycast(ray, out hit, 30))
    10.             {
    11.  
    12.                 if (hit.collider.tag == "Switch")//Deactivate the switch
    13.                 {
    14.                     hit.collider.gameObject.GetComponent<MeshRenderer>().material.color = new Color32(0, 255, 0, 255);
    15.                     hit.collider.gameObject.GetComponent<Collider>().enabled = false;
    16.                 }
    17.                 if (hit.collider.tag == "Button")//deactivate the button
    18.                 {
    19.                     hit.collider.gameObject.GetComponent<MeshRenderer>().material.color = new Color32(0, 255, 0, 255);
    20.                     hit.collider.gameObject.GetComponent<Collider>().enabled = false;
    21.                 }
    22.             }
    23.         }
    24.         if (Input.GetMouseButtonUp(0))
    25.         {
    26.             hit.collider.gameObject.GetComponent<MeshRenderer>().material.color = new Color32(255, 0, 0, 255);
    27.             hit.collider.gameObject.GetComponent<Collider>().enabled = true;
    28.         }
    29.     }
     
  2. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Hello, you could store the object.

    Code (CSharp):
    1. GameObject tempObject;
    2.  
    3. // then in the raycast
    4.  
    5. tempObject = hit.collider.gameobject;
    6.  
    My guess is that a few frames pass between the time the mouse is down, and up again.
     
    realdka13 likes this.
  3. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
  4. TimmyTheTerrible

    TimmyTheTerrible

    Joined:
    Feb 18, 2017
    Posts:
    186
    One solution would be to cast a ray on mouse down, and then store the object hit by the ray, before disabling the collider. then on mouse button up, you still have the cached object and you can do your final changes.
     
    realdka13 likes this.