Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Color of a tile - Tilemaps Unity 2017.2

Discussion in '2D' started by Tyfoux, Sep 21, 2017.

  1. Tyfoux

    Tyfoux

    Joined:
    Sep 21, 2017
    Posts:
    2
    Hi guys,
    I'm new on Unity. I train myself on beta version and new tools, specialy tilemap.
    I have no experience with it, and there's not much documentation on this subject.
    I looked for a solution during several days...

    I would like to change the color of a tile when mouse is above. I used this functions :

    Code (CSharp):
    1.  
    2.     private void OnMouseOver()
    3.     {
    4.          tilemap.SetColor(tilemap.WorldToCell(Input.mousePosition), blue);
    5.     }
    6.  
    7.     void Start()
    8.     {
    9.         Tilemap tilemap = GetComponent<Tilemap>();
    10.         blue = Color.blue;
    11.     }
    12.  
    13.  
    And i got this error :

    /!\ NullReferenceException: Object reference not set to an instance of an object /!\

    I makes it probably not of the right way...
    Is it possible to make something like that?

    I saw on an another thread : Tile asset is the same in every cell.
    That means, i can't change the color of one tile specific?
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    The docs for SetColor state that it changes the color of a specific tile.
    https://docs.unity3d.com/2017.2/Documentation/ScriptReference/Tilemaps.Tilemap.SetColor.html

    Your null reference exception is likely because your script isn't finding the Tilemap component in Start, or somehow the OnMouseOver function is getting called before Start is called.

    Is this script on the Tilemap object? If not, you'll need to get the Tilemap component in a different way.

    You can try changing Start to Awake, and also adding a debug thing like this:
    Code (CSharp):
    1. void Awake
    2. {
    3.     Tilemap tilemap = GetComponent<Tilemap>();
    4.     if(tilemap == null)
    5.     {
    6.         Debug.Log(gameObject.name + " does not have a Tilemap component");
    7.     }
    8. }
     
  3. samizzo

    samizzo

    Joined:
    Sep 7, 2011
    Posts:
    487
    Your problem is that you're getting the Tilemap component reference and storing it in a local variable. I assume you also have a class instance tilemap variable that you're trying to use in OnMouseOver. So just remove the `Tilemap` keyword in `Start`.

    -sam
     
    LiterallyJeff likes this.
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Can't believe I overlooked that :D good catch
     
  5. Tyfoux

    Tyfoux

    Joined:
    Sep 21, 2017
    Posts:
    2
    Hi guys,

    Firstly thanks for your help, jeffreyschoch and samizzo !

    Eventually I've changed my plans for that problem.

    I've corrected my mistakes, but I didn't manage to use Tilemap.SetColor function... So I've used another function, SetTile.
    This, allows me when the mouse is over the tile map, to change the tile.
    Then, I've created a second tile, with the same sprite tile, with a different color.

    2017-09-26_10-49-20.png
    2017-09-26_10-49-53.png
    2017-09-26_10-50-21.png



    If anyone is interessed :
    (This script is join to the tilemap)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Tilemaps;
    5. using System;
    6.  
    7. public class changeColor : MonoBehaviour
    8. {
    9.     private Tilemap tilemap;
    10.  
    11.     public Tile normalTile;
    12.     public Tile mouseOnTile;
    13.  
    14.     private Ray ray;
    15.     private Vector3Int posMouseOnGrid;
    16.     private Vector3Int savePosMouseOnGrid;
    17.  
    18.  
    19.     private void OnMouseOver()
    20.     {
    21.  
    22.         if (savePosMouseOnGrid != posMouseOnGrid)
    23.         {
    24.             if (tilemap.HasTile(savePosMouseOnGrid))
    25.             {
    26.                 tilemap.SetTile(savePosMouseOnGrid, normalTile);
    27.             }
    28.             savePosMouseOnGrid = posMouseOnGrid;
    29.         }
    30.  
    31.         if (tilemap.HasTile(posMouseOnGrid))
    32.         {
    33.             tilemap.SetTile(posMouseOnGrid, mouseOnTile);
    34.         }
    35.     }
    36.  
    37.  
    38.  
    39.  
    40.     void Awake()
    41.     {
    42.         tilemap = gameObject.GetComponent<Tilemap>();
    43.         if (tilemap == null)
    44.         {
    45.             Debug.Log(gameObject.name + " does not have a Tilemap component");
    46.         }
    47.     }
    48.  
    49.     void Start()
    50.     {
    51.         savePosMouseOnGrid = new Vector3Int(0, 0, 0);
    52.     }
    53.  
    54.     void Update()
    55.     {
    56.         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    57.  
    58.         posMouseOnGrid = tilemap.WorldToCell(new Vector3(ray.origin.x, ray.origin.y, 0));
    59.  
    60.     }
    61. }
     
    pyrecraft likes this.
  6. MesserFechter

    MesserFechter

    Joined:
    Dec 12, 2017
    Posts:
    1
    It is likely that your tiles color is locked, since this is the default. To change this go to your tile asset under your tile pallet, look at the inspector, right click it and look at the debug inspector, and change the flag from 'color locked' to 'none'.
     
    makuk369, Switch_Z and Xx_hugo_xX like this.
  7. mrmoonshield

    mrmoonshield

    Joined:
    Feb 27, 2018
    Posts:
    2
    Just do this:
    Code (CSharp):
    1. tilemap.SetTileFlags(cellPos, TileFlags.None);
    2. tilemap.SetColor(cellPos, new Color(0.9820799f, 0.6273585f, 1));
    Its working.
     
    Luis0413 likes this.