I feel like this is a simple question but I haven’t been able to find an answer nor figure it out for myself yet. I have script on a prefab so that when there is a mouse press, the name of the object clicked will appear in the unity debug console. However, every clone of this script that get click prints that its name is the first clone instantiated. Any help would be great! Thanks in advance for your time!
Code Instantiating Clones:
public void drawMap(Transform emptyTile, Transform enemyTile, Transform heroTile)
{
int tileNumber = 0;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
tileNumber++;
GameObject t = (GameObject)GameObject.Instantiate(Resources.Load("Prefabs/EmptyTile"), new Vector3(i * tileSize, j * tileSize), Quaternion.identity, GameObject.FindObjectOfType<Canvas>().transform);
t.name = "EmptyTile #" + tileNumber;
}
}
}
Script attached to prefab:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EmptyTileScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
private void OnMouseDown()
{
Debug.Log("I am an EmptyTile and my Name is " + name);
}
}