• 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 manu_1980_ · Mar 11, 2013 at 06:43 AM · guitexturebuttonloopchange

change texture of GuiButton in for loop

Hello,

I´m very new to programing and started to learn c# a couple of weeks ago. I want to create a Tic Tac Toe game in Unity. So my first goal is to create a functional 3x3 board.

I use a 2d array and nested for loops to create the board. Now I´m trying to find a way, how to change the texture of one button when clicked. I´ve set up an array of textures, so I can use the indexes for each texture (X,O and blank).

So, my problem is.. Because the Buttons are drawn in the for loop, when I change the index via mouseclick, every Button gets the same texture. I need a way to assign the texture after the for loop. Can you help me with this?

Thanks,

Manuel

 using UnityEngine;
 using System.Collections;
 
 public class buttons : MonoBehaviour {
     
 //Variables////////////////////////////////////////////////////////
 //
     
     public GUISkin customSkin;
     
     public Texture2D[] buttonTex;
     
     int playerX =     1;
     int playerO =     2;
     int blank    =      0;
 
     
     int turn = -1;
     int state = 0;
     
     public int x;
     public int y;
     
 
 
     
 //Game Board///////////////////////////////////////////////////////////////////////////////////
     //
     
     void OnGUI (){
         
     //Custom Skin
         //
         
     GUI.skin = customSkin;
         
         
 
         // Draw 3x3 Board
         //
         
 
         
     int index = 0;
         
     int [,] board = new int [3,3];
         
     for (y = 0; y < 3; y++)
     {
     for (x = 0; x < 3; x++)
         {
             if (GUI.Button (new Rect (x *100,y *100, 100, 100), new GUIContent (buttonTex[state + 1])))
                     
             {
                 
                 transform.localScale = new Vector3(0.1F, 0, 0);
                 state = turn;
                 turn = turn * -1;
                 
                 
             }    
         }
 
     }            
         
 }
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

  • Sort: 
avatar image
Best Answer

Answer by robertbu · Mar 11, 2013 at 07:01 AM

You need to keep track of the state of each button individually. Here are a few changes to your code. I'm not sure if this is what you were going to use board[,] for.

 using UnityEngine;
 using System.Collections;
  
 public class buttons : MonoBehaviour {
  
 //Variables////////////////////////////////////////////////////////
 //
     public GUISkin customSkin;
  
     public Texture2D[] buttonTex;
  
     int playerX =  1;
     int playerO =  2;
     int blank  =    0;
  
  
     int turn = -1;
     int state = 0;
  
     public int x;
     public int y;
     
     private int [,] board = new int [3,3] {{0,0,0},{0,0,0},{0,0,0}};
  
 //Game Board///////////////////////////////////////////////////////////////////////////////////
     //
  
     void OnGUI (){
  
     //Custom Skin
        //
  
     GUI.skin = customSkin;
  
        // Draw 3x3 Board
        //
  
     int index = 0;
 
     for (y = 0; y < 3; y++)
     {
     for (x = 0; x < 3; x++)
        {
          if (GUI.Button (new Rect (x *100,y *100, 100, 100), new GUIContent (buttonTex[board[y,x]])))
  
          {
           //transform.localScale = new Vector3(0.1F, 0, 0);
           //state = turn;
           //turn = turn * -1;
             board[y,x] = (board[y,x] + 1) % 3;
          }    
        }
  
     }       
 }
     
 }
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 manu_1980_ · Mar 11, 2013 at 11:01 PM 0
Share

Thanks a lot for your help! I will take a look at your code and try to figure out, what is going on. I know this is very basic stuff. But these are my first steps in coding/scripting.. and I try, to make my own Tic Tac Toe, without copying one of many TTT tutorials out there.

So be prepared .. I will get stuck many more times ;)

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Button Texture Issues?? 1 Answer

Change Text of GUI Button from Script 2 Answers

proper inventory system issue.. 1 Answer

how to change a gui button texture 1 Answer

Changing one button style's background changes for all styles 2 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