Keeping track of objects

Hi there

I have 5 prefabs and 5 buttons. Each button will instantiate its own prefab (one of the 5). When I click on the next button, the last created prefab must be Destroyed. My question is this: How can I keep track what the last prefab is that was created, so that I can tell unity to destroy that prefab.

Thanks in advance

Instantiate() returns a reference to the newly created object, so you simply need to store a reference to that object somewhere. (As for where to store it, that’s entirely up to you.)

The mistake I was making is that I declared the variable as an GameObject. Declaring it as a Transform solved the problem…

You can declare it as a GameObject. (If the prefab is stored as a GameObject reference, Instantiate() should return a GameObject reference, which can then be assigned to a variable of type GameObject.)

I thought I had this sorted, but not lucky yet! Please see if you guys can spot what is going on here!!! I made comments to try and make it easier:

//this variable make sure that the reference object does not get created on every update and is updated from the GUI buttons
static var instOnlyOnce:boolean = false;
var tempObject:GameObject;
var lastCreated:GameObject;

function Update()
{
	
	if(instOnlyOnce)
		{
			tempObject = GameObject.Find("toolsHolder").GetComponent("scoreGUI").curSelection;
			var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			var hit: RaycastHit;
			Instantiate(tempObject,hit.point,Quaternion.identity);
			instOnlyOnce = false;
		}	
}

function OnMouseDown () {

if(GameObject.Find("toolsHolder").GetComponent("scoreGUI").readyToPlaceObject == true)
	
	{
		//This is the part that I don't understand why it does not work. I get an error saying that 'Destroying asset is not permitted to avoid data loss'
		Destroy(tempObject);
		var currentSelectionB:GameObject = GameObject.Find("toolsHolder").GetComponent("scoreGUI").curSelection;
		var ray = Camera.main.ScreenPointToRay (Input.mousePosition); 
		var hit : RaycastHit; 
		if (Physics.Raycast (ray, hit)) 
			
			Instantiate(currentSelectionB,hit.point, Quaternion.identity); 
			GameObject.Find("toolsHolder").GetComponent("scoreGUI").readyToPlaceObject = false;
			GameObject.Find("toolsHolder").GetComponent("scoreGUI").initTextures();
	}

}

What is ‘curSelection’?

Also, I strongly suggest replacing hard tabs with spaces before posting code, as it will (often) make it much easier to read.

Sorry about the tab thing, I will remember that for next time.

curSelection (current Selection) is a static var in my GUI script that gets populated when the player click on one of the 5 buttons. You see, wat I ultimately want is this:

  1. User click on a button,
  2. object gets instantiated and follow mouse,
  3. when user is happy with the position of the object, he clicks again and the object stay in that position.

Hope this helps

Ok, but what is the type of curSelection?

(I don’t use UnityScript so I can’t really tell you what’s going on here. But, it looks like the only call to Destroy() you have in that code is Destroy(tempObject), so I’m wondering what ‘tempObject’ points to, and in turn, what ‘curSelection’ points to.)