Tile Map issue

Hi, im following a Lynda tutorial about making a tile map and im having a small issue in one of the scripts.
For some reason, my script doesn’t recognize a Vector2 variable called tileSize, i’ve watched over and over, rebuild the script as many times as i could but still got frustrated.

this is the code:

using UnityEngine;
using System.Collections;
using UnityEditor;

public class TilePickerWindow : EditorWindow {

public enum Scale
{
    x1,
    x2,
    x3,
    x4,
    x5
}

Scale scale;
Vector2 currentSelection = Vector2.zero;
public Vector2 scrollPosition = Vector2.zero;

  [MenuItem("Window/Tile Picker")]
public static void OpenTilePickerWindow()
{
      var window = EditorWindow.GetWindow(typeof(TilePickerWindow));
      var title = new GUIContent();
      title.text = "Tile Picker";
      window.titleContent = title;
}

void OnGUI()
{
    if(Selection.activeObject == null)
        return;

    var selection = ((GameObject)Selection.activeObject).GetComponent<TileMap>();
    
    if (selection != null)
    {
        var texture2d = selection.texture2d;
        if(texture2d != null)
        {
            scale = (Scale)EditorGUILayout.EnumPopup("Zoom", scale);
            var newScale = ((int)scale) + 1;
            var newTextureSize = new Vector2(texture2d.width, texture2d.height) * newScale;
            var offset = new Vector2(10, 25);

            var viewPort = new Rect(0, 0, position.width - 5, position.height - 5);
            var contentSize = new Rect(0, 0, newTextureSize.x + offset.x, newTextureSize.y + offset.y);
            scrollPosition = GUI.BeginScrollView(viewPort, scrollPosition, contentSize);

            GUI.DrawTexture(new Rect(offset.x, offset.y, newTextureSize.x, newTextureSize.y), texture2d);

            var tile = selection.tileSize * newScale;
            var grid = new Vector2(newTextureSize.x / tile.x, newTextureSize.y / tile.y);

            var selectionPos = new Vector2(tile.x * currentSelection.x + offset.x,
                                            tile.y * currentSelection.y + offset.y);

            var boxText = new Texture2D(1, 1);
            boxText.SetPixel(0, 0, new Color(0, 0.5f, 1f, 0.4f));
            boxText.Apply();

            var style = new GUIStyle(GUI.skin.customStyles[0]);
            style.normal.background = boxText;

            GUI.Box(new Rect(selectionPos.x, selectionPos.y, tile.x, tile.y), "", style);

            var cEvent = Event.current;
            Vector2 mousePos = new Vector2(cEvent.mousePosition.x, cEvent.mousePosition.y);
            if(cEvent.type == EventType.mouseDown && cEvent.button == 0)
            {
                currentSelection.x = Mathf.Floor((mousePos.x + scrollPosition.x / **tileSize**.x));
                currentSelection.y = Mathf.Floor((mousePos.y + scrollPosition.y / **tileSize**.y));
                Repaint();
            }

            GUI.EndScrollView();
        }
    }
}

}

The issue is on Line 72 and 73 also:
currentSelection.x = Mathf.Floor((mousePos.x + scrollPosition.x / tileSize.x));
currentSelection.y = Mathf.Floor((mousePos.y + scrollPosition.y / tileSize.y));

Im a noob, bear with me.

Sorry for my grammar and thank you for your attention.

ps: the Vector2 tileSize is inherited from other class called TileMap.

Hi,

You need to access the variable tileSize through a instance of the class TileMap, Looking at your code I’m assuming your selection object is of the type TileMap. So change the 2 lines to:

currentSelection.x = Mathf.Floor((mousePos.x + scrollPosition.x / selection.tileSize.x));
currentSelection.y = Mathf.Floor((mousePos.y + scrollPosition.y / selection.tileSize.y));

Let me know if this fixes your problem.
Greetz

it will be : -

currentSelection.x = Mathf.Floor((mousePos.x + scrollPosition.x / tile.x));
currentSelection.y = Mathf.Floor((mousePos.y + scrollPosition.y / tile.y));

since tile.x and tile.y is the new tile size that is multiplied with the scale factor…I hope this solves this issue.because the code is correct and i have completed the same Tutorial and its right… However Jesse Freeman is a great coder but his teaches with bare minimal explanation of his code and you often have to work your way out through the code and improvise.

Having said that i will buy his future courses for sure because he is a awesome guy with great coding knowledge. :slight_smile: