I have three game objects with triggers (lets call them buttons) that uses the same script. When the buttons are triggered, they instantiate a prefab (different prefabs from each button using a public variable.) Now, I want the script to check if the instantiated prefab already exists in the scene. But since the prefab has now added “(Clone)” to the end of it’s name, the script can’t find it.
public GameObject objectToSpawn;
void OnTriggerEnter(Collider other)
{
if (GameObject.Find("objectToSpawn") == null)
{
//Do stuff (includes instantiate prefab)
}
else if (GameObject.Find("objectToSpawn") != null)
{
//Do other stuff
}
}
So the problem is that the script always executes “Do stuff”, even when the prefab clone exists in the scene.
I dont’ want to refer to the prefab as “prefabname(Clone)” or refer to a specific tag, because then the script won’t work with the other buttons that instantiate different prefabs. It needs to refer to the public variable.