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 /
avatar image
0
Question by MarkR22 · Jan 01, 2017 at 03:51 AM · buttontransparencyalphafademultiple objects

Fading all but single instance of a button

I have a menu with 4 buttons. If the user taps on one, I highlight it to show that it is selected. When the user places the item corresponding to the button in the game scene, the button fades back out.

The problem I am having is that when I have a button selected and then select another button, both buttons are highlighting. The behavior I am trying to achieve is the first button fades when the second button is pressed.

I have tried setting the selectedButton to null, which calls the FadeAll() method from Update(), but both the first and second button clicked remain highlighted. When I click on the game scene, the GameObject corresponding to the latest button press is placed, and all buttons are faded.

Here is my class that controls button fading:

 using UnityEngine;
 using System.Collections;
 
 public class Button : MonoBehaviour {
 
     public static GameObject selectedButton;
     [SerializeField] private GameObject thisButton;
     private SpriteRenderer sprite;
     private Color originalColor;
     private Button[] buttons;
     private StarDisplay starDisplay;
 
     void Start () {
         starDisplay = GameObject.FindObjectOfType<StarDisplay>();
         buttons = GameObject.FindObjectsOfType<Button>();
         sprite = GetComponent<SpriteRenderer>();
         originalColor = sprite.color;
         FadeAll();
     }
     
     void Update () {
         if (!selectedButton){
             FadeAll();
         }
     }
 
     private void FadeAll(){
         foreach (Button button in buttons){
             Color fade = sprite.color;
             fade.a = 0.5f;
             sprite.color = fade;
         }
     }
 
     void OnMouseDown(){
         //TODO error, if select one then select another, both highlight
         selectedButton = null;
         if (thisButton.GetComponent<Defender>().StarCost <= starDisplay.StarCount){
             selectedButton = thisButton;
             sprite.color = originalColor;
         } 
     }
 }

This is the relevant code from the other class that fades all buttons when one object is dropped into the game scene.

 using UnityEngine;
 using System.Collections;
 
 public class DefenderSpawner : MonoBehaviour {
 
     private Vector2 targetLocation;
     private GameObject parent;
     private StarDisplay starDisplay;
     private int defenderCost;
     void Start () {
         starDisplay = GameObject.FindObjectOfType<StarDisplay>();
         parent = GameObject.Find("Defenders");
         if (!parent){
             parent = new GameObject("Defenders");
         } 
     }
 
     void OnMouseDown(){
         targetLocation = CalculateWorldPointOfMouseClick(Input.mousePosition);
         if (Button.selectedButton){
             defenderCost = Button.selectedButton.GetComponent<Defender>().StarCost;
             if (starDisplay.UseStars(defenderCost)){
                 GameObject defender = Instantiate(Button.selectedButton, targetLocation, Quaternion.identity) as GameObject;
                 defender.transform.parent = parent.transform;
                 Button.selectedButton = null;
             } else {
                 print("Cannot afford defender");
             }
         }
     }
 

In the second class, all buttons fade when the selected Button == null. But in the first class this is not happening when I set selectedButton = null in OnMouseDown(). Any ideas for another way to go about this would be greatly appreciated!

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
0

Answer by IgorAherne · Jan 01, 2017 at 09:45 AM

Notice what happens during OnMouseDown:

You set

 selectedButton = null;

Then, this block of code executes:

 if (thisButton.GetComponent<Defender>().StarCost <= starDisplay.StarCount){
              selectedButton = thisButton;
              sprite.color = originalColor;
  } 

Notice, that selected button is not null as the function exits.

Then your update will run (it's ran after all input-handling functions like OnMouseDown etc), and it sees that selected button is Not null.

So instead of fading out inside of Update, call FadeAll() as soon as you need it, during OnMouseDown() instead of selectedButton = null;

Comment
Add comment · Show 1 · 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 MarkR22 · Jan 01, 2017 at 09:52 AM 0
Share

I had FadeAll() there before but changed it to selectedButton = null when I was trying to fix my problem. FadeAll() causes the same behavior. The first button is not faded for some reason still if a second one is pressed.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Fading out a transparent material 1 Answer

Fading out doesn't fade out, but instantly goes from black to clear 0 Answers

Fade In/out issue about a gameobject 0 Answers

Shader is not displaying alpha correctly 0 Answers

Alpha not displaying when changed in code, unless changed manually in the inspector 0 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