Procedural Generated Mesh Dissapears when Mouse Moves Over

I’ve been following a youtube series on making a procedurally generated tile map. Everything has gone great so far until now. The videos were made in 2013 so I had to update my code to Unity 5 in order to work.

My problem is I generate the mesh with the texture on it. When I move the mouse over the mesh at all in game mode it dissapears. What I’m trying to do is have a 1x1 cube represent where the mouse is on the tile map.

I’m not sure what the problem could be or why it just dissapears.

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(TileMap))]
public class TileMapMouse : MonoBehaviour {

    Collider collider;
    Renderer renderer;
    TileMap _TileMap;

    Vector3 currentTileCoord;

    public Transform selectionCube;

    void Start()
    {
        collider = GetComponent<Collider>();
        renderer = GetComponent<Renderer>();
        _TileMap = GetComponent<TileMap>();
        selectionCube = GetComponent<Transform>();
    }

    void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;

        if( collider.Raycast( ray, out hitInfo, Mathf.Infinity))
        {
            int x = Mathf.FloorToInt( hitInfo.point.x / _TileMap.tileSize);
            int z = Mathf.FloorToInt(hitInfo.point.z / _TileMap.tileSize);

            currentTileCoord.x = x;
            currentTileCoord.z = z;

            selectionCube.transform.position = currentTileCoord * 5f;
        }
        else
        {

        }
    }
}

Actually, I just found out that the Tile Map when the mouse enters anywhere on it, the TileMap moves positions to
(845, 0, 745) and the cube doesnt move at all. If anybody knows why or sees a flaw in in please let me know

On line 21 you override the selectionCube transform reference, so fi you were counting on it being set manually in the inspector, it is overridden to the object that IS the map, that map may be moved by line 37.

So I shouldn’t override the transform?

Perhaps you intend to, but make sure that the GameObject this script is on (which happens to RequireComponent a TileMap, so I’m guessing it is your tilemap) is actually the one you want to move when you click. That override line is saying “point to my transform” and line 37 says “move me,” which means move the thing with the required TileMap.

Ah okay yeah what I intend to do it keep the tile map stationary but move the cube to where the mouse is over the map

I’ve tried doing a few things to move the selection cube but nothing has worked so far.

How would I grab the selection cube and move the transform on the tile map?