Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Jan 08, 2016 at 03:15 AM by usalalas for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by usalalas · Dec 19, 2015 at 01:39 AM · buttonscript.

Change UI button state to "pressed" via script

Good evening,

I have some UI buttons, but as my game is for Android, I'm having some problems with changing the state of the button, I have one sprite for the button and one for it when pressed. The problem is, I can't change the state of the button. Bellow is the script I'm using the check if the button was touched, and I'm sending a message to it. How do I change the state of this button so it can show the right sprite (Pressed)?

 void Update () {
         if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) {
             RaycastHit2D buttonHit;
             buttonHit = Physics2D.Raycast(this.transform.position, Input.touches[0].position);
             if (buttonHit)
             {
                 objectHit = buttonHit.transform.gameObject;
                 if (objectHit.name == "Button_Play")
                 {
                     objectHit.SendMessage("LoadScene",1,SendMessageOptions.DontRequireReceiver);
                 }
             }
         }
     
     }
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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by usalalas · Dec 21, 2015 at 02:28 AM

Using Event System instead.

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

Answer by hexagonius · Dec 19, 2015 at 06:53 AM

are you using Unity UI buttons? if not, why not? if yes, check this out, no code needed. Just create a UI button change transition and assign sprite, done

Sprite Swap Option on Button

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 usalalas · Dec 19, 2015 at 12:03 PM 0
Share

Good $$anonymous$$orning @hexagonius,

Thanks for the reply. And yes, I'm using UI button, the problem is, when I touch the button, the script recognize the touch, but don't know which object was touched. That is why I use the Raycast to find out which object was touched. As you see in the script, I'm already sending a message to the button. I tried to send a message "OnClick" but didn't work. If there is a better way to recognize a button touch on a Touch Screen, please let me know.

avatar image usalalas · Dec 19, 2015 at 12:08 PM 0
Share

Here is the configuration of the button.

alt text

image.jpg (37.4 kB)

Follow this Question

Answers Answers and Comments

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

Related Questions

activate button on triggerenter 0 Answers

I want to press a Button and play a sound? 2 Answers

Spawn prefab on UI button click 0 Answers

Unity Button Script 0 Answers

Making an object move non stop after button input? 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