The last time I posted this it was closed because a moderator misread a comment of mine referring to an issue I found the solution to while writing the post about the post, not the solution to the issue in Unity.
The post went as follows:
Unity being pain rn
I’m a true beginner and having trouble getting code from a tutorial to work. Am planning to go back later to understand it all when this system works, but I can’t get this error to go away and have no idea what’s wrong.
error is line 18, coords are (18,13)
the tutorial: 3D Grid Building System - Unity Tutorial | City Builder, RTS, Factorio - YouTube
The code: (BuildingSystemGround)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class BuildingSystemGround: MonoBehaviour
{
public static BuildingSystemGround current;
public GridLayout gridLayout;
private Grid grid;
[SerializeField] Tilemap MainTilemap;
[SerializeField] TileBase whitetile;
public GameObject prefab1;
public GameObject prefab2;
private PlaceableObject objectToPlace;
#region Unity methods
private void Awake()
{
current = this;
grid = gridLayout.gameObject.GetComponent<Tilemap1>();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
InitializeWithObject(prefab1);
}
else if (Input.GetKeyDown(KeyCode.B))
{
InitializeWithObject(prefab2);
}
}
#endregion
#region Utils
public static Vector3 GetMouseToWorldPosition()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit raycastHit))
{
return raycastHit.point;
}
else
{
return Vector3.zero;
}
}
public Vector3 snapCoordinateToGrid(Vector3 position)
{
Vector3Int cellPos = gridlayout.WorldToCell(position);
position = grid.GetCellCenterWorld(cellPos);
return position;
}
#endregion
#region BuildingPlacement
public void InitializeWithObject(GameObject prefab)
{
Vector3 position = snapCoordinateToGrid(Vector3.zero);
GameObject obj = Instantiate(prefab, position, Quaternion.identify);
objectToPlace = obj.GetComponent<PlaceableObject>();
obj.AddComponent<ObjectDrag>(objectToPlace);
}
#endregion
}
(end of "BuildingSystemGround)
have slightly more code here under another script, ObjectDrag:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectDrag : MonoBehaviour
{
private Vector3 offset;
private void OnMouseDown()
{
offset = transform.position - BuildingSystem.GetMouseToWorldPosition();
}
private void OnMouseDrag()
{
Vector3 pos = BuildingSystem.GetMouseToWorldPosition() + offset;
transform.position = BuildingSystem.current.SnapCoordinateToGrid(pos);
}
}
(end of “ObjectDrag”)
SORRY THE PROGRAM MESSED UP THE CODE THERE ARE HASHTAGS (this was the problem with the editor I had said I found the solution to by noticing the “insert code snippet” button in the text editor here in my comment. The moderator completely misread it and I ended up getting NO answer to my ACTUAL issue)
The space:

