Some weirdness with recursive instantiation.

I have an object which when clicked, will instantiate another prefab of itself via Resources.Load & Instantiate. The problem is that subsequent clicks to the object (or instantiated clones) clones all of them, quickly multiplying.

It feels like one of two things are happening (or possibly both)

  1. The mouse raycast is somehow hitting the colliders of multiple objects (Can I “consume” the ray?)
  2. The prefab instantiating itself is somehow causing recursion (or is affecting the actual prefab)

What can I do to enable an object to instantiate itself without this multiplying effect?

Code is beneath
using UnityEngine;
using System.Collections;

public class CubeBuilder : MonoBehaviour {
	
	public Camera camera;
	public GameObject prefabToInstance;
	
	// Use this for initialization
	void Start () {
		camera = Camera.main.camera;
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetMouseButtonUp(0))
		{
			Ray t = camera.ScreenPointToRay(Input.mousePosition);
			RaycastHit rh = new RaycastHit();
			
			if (Physics.Raycast(t,out rh))
			{				
				Instantiate(prefabToInstance,transform.position+new Vector3(0,0.1f,0),transform.rotation);
			}	
		}
	}
}

This code is placed on a cube, with a collider (size < 0.1f), cube is made into prefab, and prefab is linked to itself (GameObject reference)

My guess is it’s the first one, and you’re using Collider.Raycast which is where each object determines for itself it it’s been hit. You might look into Physics.Raycast which if I’m not mistaken, will return the first (closest) object hit. But I’d need to see your code to be sure.

Solved: My prefab had the script above on it. This was causing the script to be instantiated with it, causing multiple copies of the script to fire each time an object was clicked. Solution was to remove the script from the prefab.