0

Hello below is the code when my character moves and 2 or more objects come into the field of view of raycast, the GUI element overlap the their name. I have tried many options of GUI but not succeeded, please tell me what could be the right approach.

using UnityEngine;
using System.Collections;

public class rayss : MonoBehaviour 
{
FOV2DEyes eyes;
FOV2DVisionCone visionCone;
float speed = -5;
RaycastHit hit;
RaycastHit[] objects ;
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);
}



void Update()
{
    objects  = new RaycastHit[eyes.hits.Count];



    int i = 0;
    foreach (RaycastHit hit in eyes.hits)
    {
        if (hit.transform)
        {
            objects[i] =hit ;
            i++;
        }

    }

}




void OnGUI()
{

    foreach (RaycastHit hit in objects)
    {


        GUI.Label( new Rect(40, 25, 100, 40), hit.collider.gameObject.name);

    }


}

}

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Anas
  • 25
  • 1
  • 5
  • I would recommend you debugging the code and evaluating variable values and see if anything stand out in regards to being unusual – MethodMan Feb 01 '15 at 18:45
  • Your GUI.Label( new Rect(40, 25, 100, 40), hit.collider.gameObject.name); has a fixed positioned Rectangle. So, all the name shown in that fixed position of the screen. Try dynamic values there which will change for each gameobject in your foreach (RaycastHit hit in objects){...} method. – Imtiaj Ahmed Feb 01 '15 at 18:56

1 Answers1

0

It's simple OnGUI you can change your code to this:

void OnGUI()
{
    float yHeight = 25;
    foreach (RaycastHit hit in objects)
    {
        GUI.Label( new Rect(40, yHeight, 100, 40), hit.collider.gameObject.name);
        yHeight += 40 + 5// Label Height + some padding
    }
}

If your object list too much you need to add scroll view Like this.

Barış Çırıka
  • 1,570
  • 1
  • 15
  • 24
  • thanks a lot Baris it works but a small issue come it print 1 object too many times i think because of update after each frame i add print command to see. If 2 objects come in raycast, it print 28 times the name of first object and 28 times the second – Anas Feb 02 '15 at 21:07
  • is it possible that the GUI show the name once per event ? – Anas Feb 02 '15 at 21:23
  • I think it is algorithmic issue. in object list there are 27 dublicated objects both of object 1 and 2. I think you need to clear your list in FOV2DEyes. – Barış Çırıka Feb 02 '15 at 22:08
  • yeah may be but i am not sure because i do define any list of objects anywhere. The algorithm find the name from the gameobject dynamically in the scene but thanks a lot – Anas Feb 03 '15 at 16:41
  • my reputation is not enough to give you positive rate for answer :-) – Anas Feb 03 '15 at 16:43
  • No problem :) Nice to help you. – Barış Çırıka Feb 03 '15 at 17:03