• 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
Question by munggol97 · Nov 27, 2018 at 01:52 PM · buttoncolor changeconditionconditionalfindobjectoftype

How to change color on multiple game object when button is clicked

I have a multiple button that has an image in it, whenever a button is being clicked it changes color, but I have a condition to test and I also want to change the color of the Image attached to the Button objects when the condition is false. But if the condition is true I want to destroy the all the clicked Button Objects and Instantiate new ones. How would I properly do that? I have tried so many ways, but it all doesn't work.

The Condition on my game manager script:

 public void checkword()
     {
         wordBuilded = displayer.text.ToString();
 
        if (txtContents.Contains(wordBuilded))
        {     
             Debug.Log(wordBuilded + " Is on the list");
             FindObjectOfType<LetterTiles>().validWord();
        }
        else
        {        
            Debug.Log("Someting is wrong..");
            FindObjectOfType<LetterTiles>().invalidWord();
         }
     }

The script attached to every Button object:

 using UnityEngine;
 using UnityEngine.UI;
 
 
 public class LetterTiles : MonoBehaviour {
 
     public string letter;
 
     public GameObject tile;
 
     Color32 tColor = new Color32(255, 215, 86, 255);
     Color32 origColor = new Color32(255, 250, 210, 255);
 
     public void letterClick()
     {
         gameManager.currentWord += letter;
         gameObject.GetComponent<Image>().color = tColor;
     }
 
     public void invalidWord()
     {
         gameObject.GetComponent<Image>().color = origColor;        
     }
     public void validWord()
     {
         Destroy(gameObject);
     }
 
     
 
 }



Comment

People who like this

0 Show 0
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

Answer by Legend_Bacon · Nov 27, 2018 at 02:07 PM

Hello there,


The immediate way to make t$$anonymous$$s work would be to use FindObjectsOfType(), instead of FindObjectOfType() (note the extra "s").


T$$anonymous$$s returns an array of all enabled objects of your chosen type in the scene. Once you have that, all you have to do is iterate through it and call the appropriate function.


 LetterTiles[] tilesArray = FindObjectsOfType<LetterTiles>();
 
 foreach (LetterTiles item in tilesArray)
 {
       if(InsertYourConditionHere) // Is the word valid or not?
            item.validWord();
       else
            item.invalidWord();
 }


Somet$$anonymous$$ng like t$$anonymous$$s should do it.


I hope that helps!

Cheers,

~LegendBacon

Comment

People who like this

0 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 munggol97 · Nov 28, 2018 at 12:12 AM 0
Share

Thanks for looking out on my question but the problem in this is, it destroys and change the colors of all the game objects I only want that to happen on the ones that has been clicked

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

113 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 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 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 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

Use a #if statement to check if a script exists in project? 0 Answers

c# Disregards my bool as if it doesn't exist? 1 Answer

Change the button target image alpha 1 Answer

Unity GUILayout Button 3 Answers

Why is door opening so fast with boolean condition? 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