- Home /
Raycast and GUI question
I have been going through the forums and Google and can't seem to find a good answer for this. I have a tower defense game where after you click and place the tower you can click it again and a menu pops up, that all works fine, it uses raycasting and hit's. My issue is that within the towers menu I have a test button and when I try to click it the raycast shoot's through it and turns the menu off.
I have tried doing a state machine with a boolean and that didn't work and I also tried the rect.contains function that also didn't work. Does anyone have an idea of how to stop this then?
EDIT: Here is the code for the script I am trying to get it to work for. This is after the tower has been placed.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlaceableBuilding : MonoBehaviour {
[HideInInspector]
public List<Collider> colliders = new List<Collider>();
private bool isSelected;
private string nameHolder;
private bool overGUI = false;
TurretProperties tp;
Rect gRect1 = new Rect(0,200,100,50);
void Start(){
tp = GetComponent("TurretProperties")as TurretProperties;
}
void OnMouseEnter(){overGUI = true;}
void OnMouseExit(){overGUI = false;}
void Update(){
if(Input.GetMouseButton(0) && !overGUI){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit)&&isSelected){
nameHolder = hit.collider.name;
}
}
}
void OnGUI(){
if(isSelected){
if(nameHolder == "Tower1"){
GUI.Label(new Rect(0,0,100,30), nameHolder);
GUI.Label(new Rect(0,60,100,30), " " +tp.TowerDamage );
if(GUI.Button(gRect1," Plus 1")){
Debug.Log ("im clicked!!!");
}
}
if(nameHolder == "Tower2"){
GUI.Label(new Rect(0,0,100,30), nameHolder);
}
if(nameHolder == "Tower3"){
GUI.Label(new Rect(0,0,100,30), nameHolder);
}
}
}
void OnTriggerEnter(Collider c){
if(c.tag == "Building") {
colliders.Add(c);
}
}
void OnTriggerExit(Collider c){
if(c.tag == "Building") {
colliders.Remove(c);
}
}
public void SetSelected(bool s){
isSelected = s;
}
}
State machine or rect contains can both work. If you provide your code, someone can figure out how to fix your problem.
Answer by robertbu · Jul 30, 2013 at 03:30 AM
I'm not real experienced with GUI since we use EZGUI, but I believe this will do what you want:
Rect rect1 = new Rect(0,0,100,30);
Rect rect2 = new Rect(0,60,100,30);
void OnGUI(){
Event e = Event.current;
if (e.type == EventType.repaint) {
overGUI = false;
if (isSelected) {
if (rect1.Contains(e.mousePosition)) {
overGUI = true;
}
if (nameHolder == "Tower1" && rect2.Contains (e.mousePosition)) {
overGUI = true;
}
}
}
if(isSelected){
if(nameHolder == "Tower1"){
GUI.Label(rect1, nameHolder);
GUI.Label(rect2, " " +tp.TowerDamage );
if(GUI.Button(gRect1," Plus 1")){
Debug.Log ("im clicked!!!");
}
}
if(nameHolder == "Tower2"){
GUI.Label(rect1, nameHolder);
}
if(nameHolder == "Tower3"){
GUI.Label(rect1, nameHolder);
}
}
}
Since OnGUI() can get called multiple time per frame, we use the repaint event to assure we only calculate 'overGUI' once.
Your answer
![](https://koobas.hobune.stream/wayback/20220613111141im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
GUI, SpellBar and C#-related questions 1 Answer
How to check if a button left the boundaries of a Viewport in ScrollRect? 1 Answer
OnMouseUp() not working like meant 1 Answer
Distribute terrain in zones 3 Answers