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 LT23Live · Feb 07, 2014 at 12:15 AM · onclickgui-texture

Detect Mouse Click on GUI Texture (NOT ONGUI)

Hi everyone, I am new to Unity, and Unity Answers.

I am creating a stat selection screen for homework. I am confused about writing a line of code that is probably very simple. I want to detect a click on the GUI Texture without using on Gui if its possible. :)

I have searched for the post that can help me but I do not think anyone is in my situation.

I hope somebody can help! :) Thanks for your time.

 using UnityEngine;
 using System.Collections;
 
 public class StatScript : MonoBehaviour {
     private int[] stats = new int[] {5,5,5,5,5,5};
     private int pointSpend = 25; 
     public GUIText points;
 
     public GUIText strength;
     public GUITexture strengthSpend;
     public GUITexture strengthReturn;
 
     public GUIText stamina;
     public GUITexture staminaSpend;
     public GUITexture staminaReturn;
 
     public GUIText constitution;
     public GUITexture constitutionSpend;
     public GUITexture constitutionReturn;
 
     public GUIText agility;
     public GUITexture agilitySpend;
     public GUITexture agilityReturn;
 
     public GUIText intellegence;
     public GUITexture intelligenceSpend;
     public GUITexture intellegenceReturn;
 
     public GUIText willpower; 
     public GUITexture willpowerSpend;
     public GUITexture willpowerReturn;
 
     public AudioClip selectTone;
 
 
     // Update is called once per frame
     void Update () {
                 points.text = pointSpend.ToString (); 
                 strength.text = stats [0].ToString ();
                 stamina.text = stats [1].ToString ();
                 constitution.text = stats [2].ToString ();
                 agility.text = stats [3].ToString ();
                 intellegence.text = stats [4].ToString ();
                 willpower.text = stats [5].ToString ();
 
         }
 
     void OnMouseUp(){
         audio.PlayOneShot (selectTone);
 
         if (strengthSpend)){
             stats[0]= stats[0] +1;
             pointSpend = pointSpend -1; 
         }
 
         else if  (strengthReturn){
             stats[0]= stats[0] -1;
             pointSpend = pointSpend +1; 
         }
 
         else if  (staminaSpend){
             stats[1]= stats[1] +1;
             pointSpend = pointSpend -1; 
         }
 
         else if  (staminaReturn){
             stats[1]= stats[1] -1;
             pointSpend = pointSpend +1;
         }
 
         else if  (constitutionSpend){
             stats[2]= stats[2] +1;
             pointSpend = pointSpend -1; 
         }
 
         else if  (constitutionReturn){
             stats[2]= stats[2] -1;
             pointSpend = pointSpend +1; 
         }
 
         else if  (agilitySpend){
             stats[3]= stats[3] +1;
             pointSpend = pointSpend -1; 
         }
 
         else if  (agilityReturn){
             stats[3]= stats[3] -1;
             pointSpend = pointSpend +1; 
         }
 
         else if  (intelligenceSpend){
             stats[4]= stats[4] +1;
             pointSpend = pointSpend -1; 
         }
 
         else if  (intellegenceReturn){
             stats[4]= stats[4] -1;
             pointSpend = pointSpend +1; 
         }
 
         else if  (willpowerSpend){
             stats[5]= stats[5] +1;
             pointSpend = pointSpend -1; 
         }
 
         else if  (willpowerReturn){
             stats[5]= stats[5] -1;
             pointSpend = pointSpend +1; 
         }
     }    
 }
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Feb 07, 2014 at 12:26 AM

You can use GUITexture.HitTest(). This function takes a screen position. So for example if you wanted to test if 'staminaReturn' was under the mouse, you could do:

 if (staminaReturn.HitTest(Input.mousePosition) {
     Debug.Log("I'm over 'Stamina Return'");
 }

Comment
Add comment · Show 2 · 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
avatar image LT23Live · Feb 07, 2014 at 02:29 AM 0
Share

@Robertbu thank you for such a quick response. It worked but i found out i offset my GUI Textures so they were not showing up in their original location. :/ So now that I am reorganizing them this is working perfectly.

Thank you so very much

avatar image luckynamal · Apr 29, 2015 at 03:04 PM 0
Share

@Robertbu thank you so so so so so so so much.... working perfect

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

18 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 avatar image avatar image avatar image

Related Questions

Any way to show a picture on the screen when a model is clicked? 2 Answers

Passing through a GameObject/Function to a button's OnClick 1 Answer

Press button on canvas to transform prefab clone? 0 Answers

GUI button to Unity5 Button OnClic function 1 Answer

Button OnClick() after Scene restart is Missing (Object) 2 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