Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 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.

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

Instantiate object in for loop with array 2 Answers

Instantiate for loop skips some objects 1 Answer

How can I naming each instantiated object in "for" loop ? 1 Answer

Having multiple objects fire prefabs in different times C# 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