Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Snipejawnkid7 · Jul 29, 2013 at 10:57 PM · c#guiraycast

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;
     }    
     
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Jul 29, 2013 at 11:00 PM 0
Share

State machine or rect contains can both work. If you provide your code, someone can figure out how to fix your problem.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges