Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 TempestInATeacup · Jul 22, 2015 at 07:05 PM · c#uibuttonactivation

Simple button script help

I'm trying to make a button that will activate an image on the first click and deactivate it if clicked again. I don't understand how to make custom scripts for buttons, what is a simple script that will achieve this and how to i attach the image i want activated? I'm coding in c#

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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by alexi123454 · Jul 22, 2015 at 07:13 PM

All you would have to do is make a script with a boolean variable, and a function for the button to use. Something like this:

 bool showImage = false;
 public GameObject displayImage;
 
 public void ToggleButton()
 {
     showImage = !showImage ;
 }
 
 void Update()
 {
     if (showImage)
     {
         displayImage.SetActive(true);
     }
     else
     {
         displayImage.SetActive(false);
     }
 }


If you're using the new unity UI (with a Canvas), you can make the UI button call the "ToggleButton" function by scrolling down to the "OnClick" list, drag in whatever gameObject has this script on it, and then point it at at the correct function. Also, make sure to assign the displayImage variable from the editor.

If you're using the old unity OnGUI system, just wrap the button with an if statement, and inside call the ToggleButton() function from inside the if statement.

Comment
Add comment · Show 3 · 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 TempestInATeacup · Jul 22, 2015 at 07:38 PM 0
Share

I've copied this into my script and added the script to the image, then dragged the image into the onclick list and selected the toggle button function. Nothing happens when I click the button still, and I don't know enough about coding to figure out why.

avatar image alexi123454 · Jul 22, 2015 at 07:43 PM 0
Share

This script has to be on something other than the image, because it's setting the gameObject to inactive when the image isn't being shown. If the game object is inactive, then the scripts on it don't get updated. Place this script on the button, and that should help.

Also, don't forget to the assign the "displayImage" variable to the image.

avatar image TempestInATeacup · Jul 22, 2015 at 07:49 PM 0
Share

it works perfectly now! thanks

avatar image
0

Answer by ToyoEmpire · Jul 23, 2015 at 12:51 AM

Hello TempestInATeacup

Searching for function Button click on C#
this a simple code to make a click and show an image, just make a new scene for image and you need to make a button again in Image scene with transparency set to 0

this tutorial will help you
https://www.youtube.com/watch?v=kQ2Qc_0MIyI

hope it help you :)

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

24 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

Related Questions

Multiple Cars not working 1 Answer

Scripting button functionality inside class with created object's method 1 Answer

HELP how to use buttons 1 Answer

Unity - keep created buttons after quit 1 Answer

Implement UI buttons in Input Manager 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