0
using UnityEngine;
using System.Collections;
public class GuardSample : MonoBehaviour 
{
FOV2DEyes eyes;
FOV2DVisionCone visionCone;
float speed = -5;
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;
        }
    }

}

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);
    }

}

}

My gaurd moves and when the player comes inside raycast, the GUI appear but name does not identify

everything works except "hit.collider.gameObject.name" Unity gives error that "object reference not set to an instance of an object"

please have a look i am new in Unity and C#

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Anas
  • 25
  • 1
  • 5

1 Answers1

2

The problem is that you do not have access to hit variable, because you are not storing it, you are only saving it in each iteration. A simple solution to that is to store the hit which makes playerInView = true;.

Something like this modifications to your Update() and OnGUI() methods must works;

RaycastHit hittenGo; // Declare up this variable

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

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

Don't mind asking me any doubt you have about my approach.

Unai
  • 436
  • 3
  • 8
  • hittenGo is a default variable of Unity or C# or what ? just to learn – Anas Jan 20 '15 at 17:25
  • another question if you dont mind plz in one frame it is only showing the name of one object even if there are two objects when one object escape from the field of view of raycast only then raycast identify other object i hope i ask question understandable :-) – Anas Jan 20 '15 at 17:36
  • 1
    You are welcome! hittenGo is just a variable that I have defined so that I can use for whatever I want. – Unai Jan 20 '15 at 19:11
  • 1
    Responding to the second comment, in that case you must create an array to store all the hitten GO. With your code you are just storing the last hitten one. So instead of creating a variable RayCastHit named hittenGo you have to create a variable wich must be an array of RayCastHit. – Unai Jan 20 '15 at 19:17
  • 1
    One more comment, hittenGo is the name of the variable which is a RayCastHit variable, where RayCastHit is a Unity engine Class. I suggest you to take a look at this Unity tutorials http://unity3d.com/learn/tutorials/modules/beginner/scripting – Unai Jan 20 '15 at 19:25
  • i add array as following RaycastHit hittenGo = new RaycastHit(); . . . . void OnGUI() { if (playerInView) { foreach (RaycastHit hittenGo in eyes.hits) { GUI.Label( new Rect(40, 25, 100, 40), hittenGo.collider.gameObject.name); } } } now it can detect both objects at a time but one strange thing happened now my objects only detect at extreme left edge of FOV rays please have a look to the images of my scene https://dl.dropboxusercontent.com/u/8025376/1.jpg https://dl.dropboxusercontent.com/u/8025376/2.jpg – Anas Jan 21 '15 at 12:53
  • i tried many options but may be due to very little understanding of error this is happening sory about that – Anas Jan 21 '15 at 12:58
  • 1
    Hi, You're not declaring an array you're just initializing an object. Here you have an example of how to create an array and how to loop it to get all of its components. https://unity3d.com/es/learn/tutorials/modules/beginner/scripting/arrays. In the example the code shows how to create an array of Gameobjects and iterate it. Yo have to do that with RayCastHit instead of GameObjects. – Unai Jan 21 '15 at 14:12