- Home /
Prefab script on clones only reference the first clone instantiated
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);
}
}
Answer by csalzman · Apr 15, 2017 at 12:18 AM
If you pause the game after the objects are instantiated does each tile have the "correct" name in the inspector?
If they are all the correct name, are you sure that the tiles (or their colliders) aren't being drawn on top of each other? The click would only register on the topmost item.
Yes I figured it out. It was the size of the collider and the way I was using my camera. I was using screenspace overlay which meant I needed to use different coordinates I guess? Either way I changed the camera to World Space and everything worked great! Thank you!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
SendMessage static parameters checking 0 Answers
How to destroy the first clone of a UI Image using an array every time I press a button? 1 Answer
deactivated script firing 0 Answers