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