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 bartleycollin · May 06, 2014 at 01:45 PM · c#guiguitextureclickpublic

How do I script a GUI Texture as a button, if it's being called from a public variable? [C#]

I have a simple UI Set up, that uses a few GUI Texture on Screen, to act as the player controls. I want to simply say, "When this GUITexture is Pressed, Do this". Here's my problem. To make it way easier on me, I'm using Public variables in a "Master Script". So I can simply drag an drop all of my textures into the inspector, and everything is set up for me. How can I call OnMouseDown (Or something similar) on these GUITexture variables? Thanks!

Comment
Add comment · Show 6
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 xortrox · May 06, 2014 at 02:12 PM 0
Share

You could assign an own script to each of the GUITexture's and have the On$$anonymous$$ouseDown inside those scripts (might require a collider per texture object), then you could either handle the click in their script or call a custom function in your master script that would handle the clicks based on what was clicked.

avatar image bartleycollin · May 06, 2014 at 02:15 PM 0
Share

I was really hoping I wouldn't have to do that.. Does anyone know any way I can keep all the code in one script?

avatar image CaioRosisca · May 06, 2014 at 02:30 PM 0
Share

Are you trying to avoid the colliders? Could cast a raycast from mouse position and use a switch to check for the texture that has been hit. By the way I would still go with xortrox's solution. You can have a script that On$$anonymous$$ouseDown calls a master script and pass as a parameter it's on gameobject, then on the master script just compare with the ones you have to check which one has been clicked.

avatar image bartleycollin · May 06, 2014 at 04:32 PM 0
Share

I decided to go with that solution. It worked, but Now I'm having a new issue (Not sure if it's related or not..) I can only click on the lower half of my button. The top half is unresponsive. Any ideas?

avatar image xortrox · May 06, 2014 at 04:41 PM 0
Share

I would suspect the collider isn't taking up the space it should other than that I'm not really sure.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by xortrox · May 06, 2014 at 02:32 PM

This is my example of how I would do it

Script per GUITexture:

 public class GUITextureClick : MonoBehaviour
 {
 
     MasterScript masterScript;
 
     void Start()
     {
 
         //Obviously has to be changed to fit your setup
         masterScript = GameObject.Find("/MyMasterScriptObject").GetComponent<MasterScript>();
 
     }
 
     void OnMouseDown()
     {
 
         MasterScript.OnMouseDown(this.gameObject);
 
     }
 
 }

Your master script would implement this function:

 public void OnMouseDown(GameObject sender)
 {
 
     switch(sender.transform.name)//One way to check which texture got clicked
     {
     
     case "Button1":
 
         break;
 
     }
 
 }

All you would have to do now is assign the GUITextureClick component to all textures, if you think THAT would be a hassle then that's also quite simple, just select all of them and assign the component through the component menu: Component->Scripts->GUITextureClick once it's added to your project.

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

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

Related Questions

Erroe when resizing GUITextures in C# file 1 Answer

How to control GUITextures within a single C# script? 1 Answer

Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer

"Top" Depth only camera Culling effects bottom camera 0 Answers

GUITexture Button? 1 Answer


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