Question by
RadioactiveD · Aug 26, 2021 at 04:33 PM ·
colorspriterenderer
Sprite goes invisible when i change it's sprite renderer color
My code spawns a 8 by 8 square of white sprites, but when I change each sprites color so that it creates a checker board all of the spawned sprites go invisible. Code to spawn :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChessBoard : MonoBehaviour
{
[SerializeField] private TileScript tile;
private int _rank, _file;
[SerializeField] private Transform _cam;
// Start is called before the first frame update
void Start()
{
CreateBoard();
}
void CreateBoard()
{
for( _file = 0; _file < 8; _file++)
{
for( _rank = 0; _rank < 8; _rank++)
{
bool colorChoice = (_file + _rank) % 2 != 0;
var spawnedTile = Instantiate(tile, new Vector3(_rank, _file), Quaternion.identity);
spawnedTile.name = $"Tile {_file}, {_rank}";
spawnedTile.InitTiles(colorChoice);
}
}
_cam.transform.position = new Vector3(4 - 0.5f, 4 - 0.5f, -10);
}
}
//code to change colors
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TileScript : MonoBehaviour
{
[SerializeField] public Color _lightCol, _darkCol;
public SpriteRenderer _spriteRend;
public void InitTiles(bool col)
{
//_spriteRend.color = col ? _lightCol : _darkCol;
}
}
The first script is on a empty object, the second script is on a tile prefab that gets instantiated.
Comment
make sure the alpha on both colours is set to 255, and not 0 since it will just make them 100% transparent