• 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 jovke998 · Feb 03, 2021 at 11:00 PM · buttongame

TicTacToe game Button color change

I am creating a simple Tic Tac Toe game in Unity 3D using only UI elements, as part of my course, still a learner. Now i wanted to add the function when i click on a button and its text property changes to X, it also changes color to red, and when it's O to change its color to green. Any ideas how i can write that script?

Here is my GameControl script if useful

 public GameObject Menu;
     public GameObject Game;
 
     public Dropdown DropdownP1;
     public Dropdown DropdownP2;
 
     public Text Instruction;
     public Text GameState;
 
     [HideInInspector]
     public string CurrentSymbol;
 
     public List<Text> Field = new List<Text>();
 
     public bool endGame = false;
 
     public AudioSource audio;
 
     public Image win;
 
     
 
     // Start is called before the first frame update
     void Start()
     {
         win.enabled = false;
 
         Menu.SetActive(true);
         Game.SetActive(false);
 
         CurrentSymbol = "X";
     }
 
     // Update is called once per frame
     void Update()
     {
         if (CurrentSymbol == "X")
         {
             if (DropdownP1.value == 0)
             {
                 GameState.text = "PLAYER 1";            
             }
 
             if (DropdownP2.value == 0)
             {
                 GameState.text = "PLAYER 2";
             }
         }
         else if(CurrentSymbol == "O")
         {
             if (DropdownP1.value == 1)
             {
                 GameState.text = "PLAYER 1";
             }
 
             if (DropdownP2.value == 1)
             {
                 GameState.text = "PLAYER 2";
             }
         }
     }
 
     public void EndTurn() 
     {
 
         
         //condition to end game
 
         //horizontal
         if (CurrentSymbol == Field[0].text && CurrentSymbol == Field[1].text && CurrentSymbol == Field[2].text)
         {
             endGame = true; 
         }
 
         if (CurrentSymbol == Field[3].text && CurrentSymbol == Field[4].text && CurrentSymbol == Field[5].text)
         {
             endGame = true;
         }
 
         if (CurrentSymbol == Field[6].text && CurrentSymbol == Field[7].text && CurrentSymbol == Field[8].text)
         {
             endGame = true;
         }
 
         //vertical
         if (CurrentSymbol == Field[0].text && CurrentSymbol == Field[3].text && CurrentSymbol == Field[6].text)
         {
             endGame = true;
         }
 
         if (CurrentSymbol == Field[1].text && CurrentSymbol == Field[4].text && CurrentSymbol == Field[7].text)
         {
             endGame = true;
         }
 
         if (CurrentSymbol == Field[2].text && CurrentSymbol == Field[5].text && CurrentSymbol == Field[8].text)
         {
             endGame = true;
         }
 
         //diagonal
         if (CurrentSymbol == Field[0].text && CurrentSymbol == Field[4].text && CurrentSymbol == Field[8].text)
         {
             endGame = true;
         }
 
         if (CurrentSymbol == Field[2].text && CurrentSymbol == Field[4].text && CurrentSymbol == Field[6].text)
         {
             endGame = true;
         }
 
         
 
 
         if (endGame == true)
         {
             if (CurrentSymbol == "X")
             {
                 if (DropdownP1.value == 0)
                 {
                     GameState.text = "PLAYER 1 WINS";
                 }
 
                 if (DropdownP2.value == 0)
                 {
                     GameState.text = "PLAYER 2 WINS";
                 }
 
                               
             }
             else
             {
                 if (DropdownP1.value == 1)
                 {
                     GameState.text = "PLAYER 1 WINS";
                 }
 
                 if (DropdownP2.value == 1)
                 {
                     GameState.text = "PLAYER 2 WINS";
                 }
             }
             sound.Play();
             win.enabled = true;
             CurrentSymbol = "END";
         }
 
 
 
 
 
         if (CurrentSymbol == "X")
             CurrentSymbol = "O";
         else if (CurrentSymbol == "O") 
             CurrentSymbol = "X";
     }
 
     public void OnStart()
     {
         Menu.SetActive(false);
         Game.SetActive(true);
 
         CurrentSymbol = "X";
         endGame = false;
 
         for (int i = 0; i < Field.Count; i++)
         {
             Field[i].text = "-";
             Field[i].GetComponentInParent<Button>().interactable = true;
 
         }
     }
 
     public void OnEndGame()
     {
         Menu.SetActive(true);
         Game.SetActive(false);
         win.enabled = false;
     }
 
     public void OnDropdownP1() 
     {
         if (DropdownP1.value == 0)
         {
             Instruction.text = "Player 1 will play first";
             DropdownP2.value = 1;
         }
         else if (DropdownP1.value == 1) 
         {
             DropdownP2.value = 0;
         }
     }
 
     public void OnDropdownP2()
     {
         if (DropdownP2.value == 0)
         {
             Instruction.text = "Player 2 will play first";
             DropdownP1.value = 1;
         }
         else if (DropdownP2.value == 1)
         {
             DropdownP1.value = 0;
         }
     }
 }


,I am creating a simple Tic Tac Toe game in Unity 3D using only UI elements, as part of my course, still a learner. Now i wanted to add the function when i click on a button and its text property changes to X, it also changes color to red, and when it's O to change its color to green. Any ideas how i can write that script?

Here is my GameControl script if useful:

     public GameObject Menu;
     public GameObject Game;
 
     public Dropdown DropdownP1;
     public Dropdown DropdownP2;
 
     public Text Instruction;
     public Text GameState;
 
     [HideInInspector]
     public string CurrentSymbol;
 
     public List<Text> Field = new List<Text>();
 
     public bool endGame = false;
 
     public AudioSource audio;
 
     public Image win;
 
     
 
     // Start is called before the first frame update
     void Start()
     {
         win.enabled = false;
 
         Menu.SetActive(true);
         Game.SetActive(false);
 
         CurrentSymbol = "X";
     }
 
     // Update is called once per frame
     void Update()
     {
         if (CurrentSymbol == "X")
         {
             if (DropdownP1.value == 0)
             {
                 GameState.text = "PLAYER 1";            
             }
 
             if (DropdownP2.value == 0)
             {
                 GameState.text = "PLAYER 2";
             }
         }
         else if(CurrentSymbol == "O")
         {
             if (DropdownP1.value == 1)
             {
                 GameState.text = "PLAYER 1";
             }
 
             if (DropdownP2.value == 1)
             {
                 GameState.text = "PLAYER 2";
             }
         }
     }
 
     public void EndTurn() 
     {
 
         
         //condition to end game
 
         //horizontal
         if (CurrentSymbol == Field[0].text && CurrentSymbol == Field[1].text && CurrentSymbol == Field[2].text)
         {
             endGame = true; 
         }
 
         if (CurrentSymbol == Field[3].text && CurrentSymbol == Field[4].text && CurrentSymbol == Field[5].text)
         {
             endGame = true;
         }
 
         if (CurrentSymbol == Field[6].text && CurrentSymbol == Field[7].text && CurrentSymbol == Field[8].text)
         {
             endGame = true;
         }
 
         //vertical
         if (CurrentSymbol == Field[0].text && CurrentSymbol == Field[3].text && CurrentSymbol == Field[6].text)
         {
             endGame = true;
         }
 
         if (CurrentSymbol == Field[1].text && CurrentSymbol == Field[4].text && CurrentSymbol == Field[7].text)
         {
             endGame = true;
         }
 
         if (CurrentSymbol == Field[2].text && CurrentSymbol == Field[5].text && CurrentSymbol == Field[8].text)
         {
             endGame = true;
         }
 
         //diagonal
         if (CurrentSymbol == Field[0].text && CurrentSymbol == Field[4].text && CurrentSymbol == Field[8].text)
         {
             endGame = true;
         }
 
         if (CurrentSymbol == Field[2].text && CurrentSymbol == Field[4].text && CurrentSymbol == Field[6].text)
         {
             endGame = true;
         }
 
         
 
 
         if (endGame == true)
         {
             if (CurrentSymbol == "X")
             {
                 if (DropdownP1.value == 0)
                 {
                     GameState.text = "PLAYER 1 WINS";
                 }
 
                 if (DropdownP2.value == 0)
                 {
                     GameState.text = "PLAYER 2 WINS";
                 }
 
                               
             }
             else
             {
                 if (DropdownP1.value == 1)
                 {
                     GameState.text = "PLAYER 1 WINS";
                 }
 
                 if (DropdownP2.value == 1)
                 {
                     GameState.text = "PLAYER 2 WINS";
                 }
             }
             sound.Play();
             win.enabled = true;
             CurrentSymbol = "END";
         }
 
 
 
 
 
         if (CurrentSymbol == "X")
             CurrentSymbol = "O";
         else if (CurrentSymbol == "O") 
             CurrentSymbol = "X";
     }
 
     public void OnStart()
     {
         Menu.SetActive(false);
         Game.SetActive(true);
 
         CurrentSymbol = "X";
         endGame = false;
 
         for (int i = 0; i < Field.Count; i++)
         {
             Field[i].text = "-";
             Field[i].GetComponentInParent<Button>().interactable = true;
 
         }
     }
 
     public void OnEndGame()
     {
         Menu.SetActive(true);
         Game.SetActive(false);
         win.enabled = false;
     }
 
     public void OnDropdownP1() 
     {
         if (DropdownP1.value == 0)
         {
             Instruction.text = "Player 1 will play first";
             DropdownP2.value = 1;
         }
         else if (DropdownP1.value == 1) 
         {
             DropdownP2.value = 0;
         }
     }
 
     public void OnDropdownP2()
     {
         if (DropdownP2.value == 0)
         {
             Instruction.text = "Player 2 will play first";
             DropdownP1.value = 1;
         }
         else if (DropdownP2.value == 1)
         {
             DropdownP1.value = 0;
         }
     }
 }
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 RealFolk · Feb 04, 2021 at 05:02 AM

 CurrentSymbol = "<color=red>X</color>";
 CurrentSymbol = "<color=green>O</color>";
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.

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

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

How To Change a Image When Press Button Click? 0 Answers

Button Opacity Animation Not Working 0 Answers

problem with button touch zone 0 Answers

how to end your game 1 Answer

Quit 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