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));
}
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).
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!
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).