Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Tilemap Color

Discussion in '2D Experimental Preview' started by senryx, Jul 28, 2021.

  1. senryx

    senryx

    Joined:
    Feb 11, 2021
    Posts:
    1
    in the script I change the color of the set block from the tilemap, but sometimes the original color appears, and only then it is repainted.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Achievements;
    4. using UnityEngine;
    5. using UnityEngine.Tilemaps;
    6. using UnityEngine.UI;
    7.  
    8. public class TileSlimeSpawn : MonoBehaviour
    9. {
    10.     private GameObject player;
    11.     public Tile[] slimeTile;
    12.     [SerializeField] private GameObject platforma;
    13.     private Tilemap slimeMap;
    14.     private Vector3Int previous;
    15.     private bool isGround, isLight;
    16.     [SerializeField] private float radiusCheck;
    17.     public LayerMask whatIsGround;
    18.     public LayerMask whatIsLight;
    19.     [SerializeField] GameObject lightForSlime;
    20.  
    21.     [SerializeField] Color color;
    22.  
    23.     private bool isCeiling = false;
    24.  
    25.     private void Start()
    26.     {
    27.         slimeMap = GetComponent<Tilemap>();
    28.         if (PlayerPrefs.GetString("SlimeColor").Length < 2)
    29.         {
    30.             color = new Color(0.1120868f, 0.754717f, 0f, 0.7176471f);
    31.         }
    32.         else
    33.         {
    34.             color = JsonUtility.FromJson<Color>(PlayerPrefs.GetString("SlimeColor"));
    35.         }
    36.  
    37.  
    38.         if (tag == "RightWall")
    39.         {
    40.             GameObject go = GameObject.Find("RightSlimeWall");
    41.             slimeMap = go.GetComponent<Tilemap>();
    42.         }
    43.  
    44.         if (tag == "LeftWall")
    45.         {
    46.             GameObject go = GameObject.Find("LeftSlimeWall");
    47.             slimeMap = go.GetComponent<Tilemap>();
    48.         }
    49.  
    50.         if (tag == "GroundCheck")
    51.         {
    52.             GameObject go = GameObject.Find("GroundSlime");
    53.             slimeMap = go.GetComponent<Tilemap>();
    54.         }
    55.  
    56.         if (tag == "Ceiling")
    57.         {
    58.             GameObject go = GameObject.Find("Ceiling");
    59.             slimeMap = go.GetComponent<Tilemap>();
    60.             isCeiling = true;
    61.         }
    62.     }
    63.  
    64.     void FixedUpdate()
    65.     {
    66.         if (slimeMap == null)
    67.         {
    68.             Start();
    69.         }
    70.  
    71.         Vector3Int currentCell = slimeMap.WorldToCell(transform.position);
    72.         isGround = Physics2D.OverlapCircle(transform.position, radiusCheck, whatIsGround);
    73.         isLight = Physics2D.OverlapCircle(transform.position, radiusCheck, whatIsLight);
    74.         slimeMap.SetTileFlags(currentCell, TileFlags.None);
    75.         slimeMap.SetColor(currentCell, color);
    76.  
    77.  
    78.         if ((isGround == true) && (isLight == false) && (currentCell != previous))
    79.         {
    80.             slimeMap.SetTile(currentCell, slimeTile[Random.Range(0, 4)]);
    81.             AchievementHandler.IncrementAchievement(Achievement.LEAVE_500_TILES);
    82.             if (isCeiling) AchievementHandler.IncrementAchievement(Achievement.LEAVE_100_TILES_ON_CEILING);
    83.             Instantiate(lightForSlime, transform.position, Quaternion.identity);
    84.             previous = currentCell;
    85.         }
    86.     }
    87. }