Search Unity

How to print raycast detected object on GUI

Discussion in 'Editor & General Support' started by ansiaries, Jan 19, 2015.

  1. ansiaries

    ansiaries

    Joined:
    Jul 22, 2014
    Posts:
    22
    Hello
    I am trying to make a scene where when raycast strike to any object, the name of that object(either one object or many) appear on GUI
    below the script i am using. the script works when i only write some text in GUI Box
    can "hit.collider.gameObject.name" call inside GUI function ?


    bool playerInView = false;

    void Update()
    {
    playerInView = false;
    foreach (RaycastHit hit in eyes.hits)
    {
    if (hit.transform && hit.transform.tag == "Player")
    {
    playerInView = true;
    }
    }
    }

    void OnGUI()
    {
    if (playerInView)
    {
    GUI.Box (new Rect (10, 10, 160, 60), hit.collider.gameObject.name);
    }
    }
     
  2. AdamScura

    AdamScura

    Joined:
    Mar 25, 2012
    Posts:
    55
    You have made several mistakes here, but it's tough to give you a straight answer without seeing the complete code. Please post the entire class file.
     
  3. ansiaries

    ansiaries

    Joined:
    Jul 22, 2014
    Posts:
    22
    using UnityEngine;
    using System.Collections;

    public class RaycastSource : MonoBehaviour
    {
    FOV2DEyes eyes;
    FOV2DVisionCone visionCone;
    float speed = -3;
    RaycastHit hit;


    void Start()
    {
    eyes = GetComponentInChildren<FOV2DEyes>();
    visionCone = GetComponentInChildren<FOV2DVisionCone>();
    }

    void FixedUpdate()
    {
    if (transform.position.x < -10 || transform.position.x > 10)
    {
    speed *= -1;
    }

    transform.position = new Vector3(transform.position.x + speed * Time.fixedDeltaTime, transform.position.y, transform.position.z);
    }

    bool playerInView = false;

    void Update()
    {
    playerInView = false;
    foreach (RaycastHit hit in eyes.hits)
    {
    if (hit.transform && hit.transform.tag == "Player")
    {
    playerInView = true;
    }
    }

    /*if (playerInView)
    {
    print ("Detected");
    }*/
    }




    void OnGUI()
    {
    if (playerInView)
    {
    GUI.Box (new Rect (10, 10, 160, 60), "Title");
    GUI.Label( new Rect(10, 10, 160, 60), hit.collider.gameObject.name);
    }

    }
    }

    when i write hit.collider.gameObject.name like above it gives error that "NullReferenceException: Object reference not set to an instance of an object"

    but when i change it to any label then the code works
     
  4. AdamScura

    AdamScura

    Joined:
    Mar 25, 2012
    Posts:
    55
    First of all, congratulations on jumping in to the wonderful world of programming! You're in the deep end of the pool, and you're sinking, but I think we can save you :).

    I read your other posts and I understand you are a student working on a college project. You don't know much about programming and you have started an ambitious project. You want to let the player walk around a virtual city, see the names of buildings, search for landmarks, etc.

    The code you posted is modified from the free FOV2D package. The way you have modified it tells me you are in trouble because you are not understanding some very basic programming concepts.

    The short answer is that you have declared 2 different variables with the same name. When you ask for the value of "hit" in the OnGUI() method, you are not referencing the same variable that was used inside the Update() method.

    Code (CSharp):
    1.  
    2. public class RaycastSource : MonoBehaviour
    3. {
    4.     ...
    5.     // Declare the first hit variable that belongs to the RaycastSource class.
    6.     RaycastHit hit;
    7.  
    8.     void Update()
    9.     {
    10.         // Declare a second hit variable that only exists inside the foreach loop.
    11.         foreach (RaycastHit hit in eyes.hits)
    12.         {
    13.             ...
    14.         }
    15.         // The second hit variable does not exist after this line.
    16.     }
    17.    
    18.     void OnGUI()
    19.     {
    20.         // The first hit variable is still null because it was never touched.
    21.         // You get an error because you're trying to access the collider property of a null value.
    22.         GUI.Label(new Rect(10, 10, 160, 60), hit.collider.gameObject.name);
    23.     }
    24. }
    25.  
    The place where a variable exists is called it's "scope". C# allows you to give 2 variables the same name if they are in a different scope. In this example, the first variable has class scope, and the second variable has local scope. We say the local-scope variable in the foreach loop is "hiding" the class-scope variable. As a beginner, you should never do this. Hiding class variables can easily confuse someone who is reading your code, so you should not do it unless there is no better way.

    I recommend you pick up a good "learning to program" book for C#. There are a lot of good ones out there. Just make sure you find one that is easy for you to understand.
     
    ansiaries likes this.
  5. AdamScura

    AdamScura

    Joined:
    Mar 25, 2012
    Posts:
    55
    Are you trying to list the names of buildings that are on screen? If so, I think there are easier & better ways to do it.