- Home /
Raycast from GUI Camera possible to only effect gui?
Hello I'm trying to create a GUI using 3D objects in front of my second Camera(GUI Cam). I'm using Raycast to determine which object I'm clicking on but I keep getting this Error.
You probably need to add a Collider to the game object "Camera". Or your script needs to check if the component is attached before using it. Camera is located away from my scene, and I'm using a script that is supposed to cast a raycast from that camera. using UnityEngine; using System.Collections;MissingComponentException: There is no 'Collider' attached to the "Camera" game object, but a script is trying to access it.
public class RaycastGUI : MonoBehaviour { public Camera camToUse; // Use this for initialization void Start() {
}
// Update is called once per frame void Update() {
if (Input.GetButton("Fire1")) {
Ray ray; RaycastHit hit; ray = camToUse.ScreenPointToRay(Input.mousePosition); if (collider.Raycast(ray, out hit, 100.0f)) {
Debug.Log(hit.transform.name); } } } } Anyone can pinpoint the problem ?
Comment
Best Answer
Answer by whydoidoit · Jan 29, 2013 at 12:17 PM
Collider.Raycast is to test against a specific collider in the world, the way you have written it means that the camera would need a collider (and it's totally not what you want anyway!). What you want to do is put the 3D GUI objects on their own layer and then use a LayerMask in a normal Physics.Raycast to limit what it can hit.
Your answer