• 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 TheRichardGamer · Apr 11, 2015 at 07:40 AM · instantiatearraysfor-loop

How do I alternate the colors of my Chessboard?

Okay, so I've managed to make my script generate a set of tiles, 8x8, as in a chessboard, but the problem is that I don't know how to alternate the colors, i.e my sprites that I have set up, on my tiles. I've tried making an int that changes and where the tiles get generated I put some if-statements but that didn't work, the pieces just turned black. I don't know how to make the colors alternate and I couldn't find anything helpful on google either. So it'd be great if you could explain to me what I need to do, and please don't post something that says that I have to do something advanced.

Anyways, here's my script so far:

 using UnityEngine;
 using System.Collections;
 
 public class Board : MonoBehaviour 
 {
 
     public Sprite wT;
     public Sprite bT;
     public GameObject emptyTile;
 
     public int[] whiteTileNums;
 
     void Start ()
     {
 
         whiteTileNums = new int[4];
         whiteTileNums[0] = 2;
         whiteTileNums[1] = 4;
         whiteTileNums[2] = 6;
         whiteTileNums[3] = 8;
 
 
         for(float x = 0; x < 8; x++)
         {
 
             for(float y = 0; y < 8; y++)
             {
 
                 
                 Vector2 newTilePos = new Vector2(x - 3.6f, -y + 3);
                 
                 GameObject newTile = (GameObject)Instantiate(emptyTile, new Vector3(newTilePos.x, newTilePos.y, 0), Quaternion.identity);
             
                 newTile.GetComponent<SpriteRenderer>().sprite = wT;
 
             }
 
         }
 
     }
 
 }
 
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 NoseKills · Apr 11, 2015 at 08:12 AM

If (x+y) is an even number, use the other tile, if not use the other.

 if ((x+y) % 2 == 0)
     newTile.GetComponent<SpriteRenderer>().sprite = wT;
 else 
     newTile.GetComponent<SpriteRenderer>().sprite = bT;
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
avatar image
0

Answer by pulkitarora · Apr 11, 2015 at 02:56 PM

//in case if it helps

using UnityEngine; using System.Collections;

public class Board : MonoBehaviour {

  public Sprite wT;
  public Sprite bT;
  public GameObject emptyTile;

public int counter = 0 ;

  public int[] whiteTileNums;
 
  void Start ()
  {
 
      whiteTileNums = new int[4];
      whiteTileNums[0] = 2;
      whiteTileNums[1] = 4;
      whiteTileNums[2] = 6;
      whiteTileNums[3] = 8;
 
 
 
      for(float x = 0; x < 8; x++)
      {
 
          for(float y = 0; y < 8; y++)
          {
 
              
              Vector2 newTilePos = new Vector2(x - 3.6f, -y + 3);

if(counter % 2 == 0 ) { //call for white/black }else { // call for the white/black } counter++;

              GameObject newTile = (GameObject)Instantiate(emptyTile, new Vector3(newTilePos.x, newTilePos.y, 0), Quaternion.identity);
          
              newTile.GetComponent<SpriteRenderer>().sprite = wT;
 
          }
 
      }
 
  }
 

}

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 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

Related Questions

Clicker game instantiate prefab depending on gold per click 0 Answers

Instantiating Prefabs from a MonoBehaviour-derived class using an array -> compiling but not rendering [SOLVED] 0 Answers

Creating text at the position of the gameobjects in world 0 Answers

Spawn an object to a random spawn point from a list 2 Answers

I'm getting an index out of range error even though my arrays are filled with elements? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges