Search code examples
unity-game-engine2drender

Prefab not rendering in unity after changing the color


I'm trying to make a tile system, and I have a function that changes the color of every other tile to make a checkerboard pattern. However, after the color is changed from white (the default) to any other color, it doesn't show up.

An image of the unity editor showing the prefab not being rendered with a color change

Grid Manager Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GridManager : MonoBehaviour
{
    [SerializeField] private int width , height;

    [SerializeField] private Tile tilePrefab;

    void Start()
    {
        GenerateGrid();
    }

    void GenerateGrid()
    {
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                var spawnedTile = Instantiate(tilePrefab , new Vector3(x , y) , Quaternion.identity);
                spawnedTile.name = $"Tile {x} {y}";

                var isOffset = (x % 2 == 0 && y % 2 != 0) || (x % 2 != 0 && y % 2 == 0);
                spawnedTile.Init(isOffset);
            }
        }
    }
}

Tile Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tile : MonoBehaviour
{
    [SerializeField] private Color baseColor , offsetColor;
    [SerializeField] private SpriteRenderer renderer;

    public void Init(bool isOffset)
    {
        if (isOffset)
        {
            renderer.color = offsetColor;
        }
        else
        {
            renderer.color = baseColor;
        }
    }
}

I've tried changing the color manually in the editor without calling the "Init" function, and that works fine.


Solution

  • enter image description here

    I think the alpha is still 0, Change it to 255..