Instantiate only one of my Prefabs

Hey all I have been trying to figure out how to do this for 3 days and I get close but still can’t get it to work properly. My problem is that I have this same script attached to 5 different GUITexts. So when I instatiate it from any guitext it spawns a five prefabs. How do I set it to where it only instantiates the prefab from the guitext i click on? also i will create mulitple clones. So i need to put a check in to see if a gameObject is spawned and destroy if instantiating a new one. Anyway any help would be great.

#pragma strict

var object : GameObject;
private var textDisplay : GUIText;
var sum : GUITexture;
var windowsum = false;
var mouseoverAudio : AudioClip;
var playerClass : Transform;


function OnMouseEnter() {
 object.guiText.material.color = Color.red;
 object.guiText.fontSize = 55;
 sum.gameObject.SetActive (true); 
 audio.clip = mouseoverAudio;
 audio.PlayOneShot( mouseoverAudio );
}

function OnMouseExit() {
 object.guiText.material.color = Color.white;
 object.guiText.fontSize = 45;
 sum.gameObject.SetActive (false);
}
 
function Update(){
	if(Input.GetMouseButtonDown(0))
	
    Instantiate(playerClass,new Vector3(249.894, 16.52124, 132.0044),Quaternion.Euler(0, -180, 0));
    
}

Use OnMouseDown, instead of checking for mouse clicks in Update.

–Eric

This solved the issue perfectly. Thank you very much. Do you know how I would go about destroying the prefab once I click on a new guitext?

You can assign the result of Instantiate to some variable, then use Destroy(someVariable.gameObject) when needed. Although if you’re not really referring to the Transform component, the type should be GameObject rather than Transform, and then you can just do Destroy(someVariable).

–Eric

var playerClassGO : GameObject = null;
function OnMouseDown() {
    if(playerClassGO == null)
    {
        playerClassGO = Instantiate(playerClass,new Vector3(249.894, 16.52124, 132.0044),Quaternion.Euler(0, -180, 0));
    }
    else
    {
        Destroy(playerClassGO);
    }
}

some thing like this

this works for single objects but I will potentially have 5 gameobjects in the scene that will all have to be destroyed to clear the area for the new one. How do I set this to an array? Do I have to use tags or can i just use some sort of [ ]?

Can anyone help me getting playerClassGO to an array? I tried to just add [ ]to the variable and if statement and about 4 other ways I found them used in the forums but I can’t get it to work!

Well, it should be var playerClass : Transform[ ]; or var playerClass : GameObject[ ];

Still dont understand what more I need to get it to work. Throws a semicolon error. If i place that it throws different errors and so on and so on.

#pragma strict

var object : GameObject;
var sum : GUITexture;
var windowsum = false;
var mouseoverAudio : AudioClip;
var playerClass : GameObject;
var playerClassGO : GameObject[];


function OnMouseEnter() {
 object.guiText.material.color = Color.red;
 object.guiText.fontSize = 55;
 audio.clip = mouseoverAudio;
 audio.PlayOneShot( mouseoverAudio );
}

function OnMouseExit() {
 object.guiText.material.color = Color.white;
 object.guiText.fontSize = 45;
 sum.gameObject.SetActive (false);
}
 
function OnMouseDown(){
	if(Input.GetMouseButtonDown(0))
	{
    	playerClassGO[] = Instantiate(playerClass,new Vector3(249.894, 16.52124, 132.0044),Quaternion.Euler(0, -180, 0));
    	 sum.gameObject.SetActive (true);
    	 PlayerClear();
	}
}

function PlayerClear() {

	

}

It should be:

playerClassGO[currentValue] = Instantiate(playerClass,new Vector3(249.894, 16.52124, 132.0044),Quaternion.Euler(0, -180, 0));

Where currentValue is an integer that you increase when you instantiate. In order to clear the entire area of the instantiated objects, you’ll need to call a function to destroy all the instantiated objects. When you do that, you’ll need to set currentValue to 0 (as there are no more objects).