Raycast hit event with array

[SOLVED] - part of
I’m looking for a way to render an object after it is hit by a raycast.
The process is:

  • The raycast hits the object
  • The objects renderer is turned off
  • The raycast leaves the object
  • The objects renderer is turned on

The only thing I need is that when the raycast leaves the object, the object will be rendered back to it’s original state.

the script

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    public class HideWalls : MonoBehaviour
    {
        public float rayLength = 100f;
        public string tagCheck = "Wall";
        public bool checkAllTags = false;
    
    
        // Update is called once per frame
        void Update ()
        {
            bool foundHit = false;
    
            RaycastHit hit; 
    
            //Raycast, limited distance
            foundHit = Physics.Raycast(transform.position, transform.forward,out hit,rayLength);
                    
            if(foundHit && !checkAllTags && hit.transform.tag != tagCheck)
                foundHit = false;
        
            if (foundHit)
            {    
                if(hit.collider !=null)
                {
                    hit.transform.GetComponent<Renderer>().enabled = false;
                }
              else
                {
                    hit.transform.GetComponent<Renderer>().enabled = true;
                }
            }
        }
    }

I am out of ideas on how to manage that, so any help would be appreciated.

ok… so what line is it complaining about with the null ref error?

The line was my try to get the hidden object back to render. I tried it with an else function and if(!foundhit), you know.
And this is were I got the NullReference. In the script above there is no line that has a NullReference error.

/////////OLD////////OLD/////////OLD///////////////
so a little update to my problem.
I solved the NullReference error but the problem itself is still there.

Updated script:

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

public class HideWalls : MonoBehaviour
{
    public float rayLength = 100f;
    public string tagCheck = "Wall";
    public bool checkAllTags = false;


    // Update is called once per frame
    void Update ()
    {
        bool foundHit = false;

        RaycastHit hit; 

        //Raycast, limited distance
        foundHit = Physics.Raycast(transform.position, transform.forward,out hit,rayLength);
                 
        if(foundHit && !checkAllTags && hit.transform.tag != tagCheck)
            foundHit = false;
     
        if (foundHit)
        {     
            if(hit.collider !=null)
            {
                hit.transform.GetComponent<Renderer>().enabled = false;
            }
            if(hit.collider == null)
            {
                hit.transform.GetComponent<Renderer>().enabled = true;
            }
        }
    }
}

I still want to call the disabled renderer to turn back on after he was hit by the raycast.

I got it! Well atleast the old one. Now one wall will not be rendered when hit by the raycast.
For the rest of the walls, nope. Now I have to put all the tagged walls in an array.
The only thing I don’t know is on how to access the renderer when the objects are in the array.
Sooo…please help?

current script:

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

public class HideWalls : MonoBehaviour
{
    public float rayLength = 100f;
    public string tagCheck = "Wall";
    public bool checkAllTags = false;
    private Renderer rend;

    void Start()
    {
        rend = GameObject.FindWithTag ("Wall").GetComponent<Renderer> (); //here's my problem
        rend.enabled = true;
    }

    // Update is called once per frame
    void LateUpdate ()
    {
        bool foundHit = false;
   
        RaycastHit hit;   

        //Raycast, limited distance
        foundHit = Physics.Raycast(transform.position, transform.forward,out hit,rayLength);

        //rend = hit.transform.gameObject.GetComponent<Renderer> ();

        Vector3 forward = transform.TransformDirection (Vector3.forward) * rayLength;
        Debug.DrawRay(transform.position, forward, Color.green);


        if (foundHit && !checkAllTags && hit.transform.tag != tagCheck)
        {
            foundHit = false;
            rend.enabled = true;
        }

        if(!foundHit)
        {       
            rend.enabled = true;
            Debug.Log("false");

            /*if(hit.collider !=null)
            {
                hit.collider.GetComponent<Renderer>().enabled = false;
            }
            //after hit, hidden Wall gets rendered
            else
            {
                return;
            }*/
        }
        else
        {
            if(foundHit)
            {
                rend.enabled = false;
                Debug.Log("hit");
            }
        }
    }

}

push for new problem. But old is solved.

Going back to your original problem and code when you do a raycast 2 things happen
The raycast returns true/false if it hit something (your foundhit)
and if it returns true the RayCast hit will be filled in with the info of the object it hit.
Otherwise it will be null.

So i think using your original code, and just adding a variable to save what was hit… It would be very easy to turn that back on when nothing was hit. Something like this:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class HideWalls : MonoBehaviour
{
    public float rayLength = 100f;
    public string tagCheck = "Wall";
   
    private Renderer lastHit;
    void Awake()
    {
            lastHit = null;
     }
    // Update is called once per frame
    void Update ()
    {
        bool foundHit = false;
        RaycastHit hit;
        //Raycast, limited distance
        foundHit = Physics.Raycast(transform.position, transform.forward,out hit,rayLength);
     
        if (foundHit && hit.transform.tag == "Wall")
        {  
                
                lastHit = hit.transform.GetComponent<Renderer>();
                lastHit.enabled = false;
         }
         else
         {
                  if (lastHit != null)
                  {
                         lastHit.enabled = true;
                         lastHit = null;
                   }
            }
        }
    }
}

that helped. Thanks alot.