- Home /
Question by
SPQR · Feb 20, 2013 at 04:03 PM ·
gameobjectprefabdestroy
Why can't I destroy my Prefab ? Please help I'm new.
using System.Collections;
using System.Collections.Generic;
public class FDEnviroment : MonoBehaviour {
//Variables Start-------------------------------------------------------------------
public enum State
{
Idle,
Initialize,
Setup,
Spawn
}
public State state;
public GameObject[] PlanetSpawanLocation;
public GameObject[] PlanetObjectPrefab;
PlayerStats MyStats;
private int MyAccountChk;
//Variables End----------------------------------------------------------------------_____________________________________
void Awake()
{
state = FDEnviroment.State.Initialize;
//Instantiate(PlanetObject,new Vector3(-0.050f,0.15f,0.59f),Quaternion.identity);
// this is needed only if you want to load from the assets file
//PlanetObject = (GameObject)Resources.Load("MyPlanet", typeof(GameObject));
}
// Use this for initialization
IEnumerator Start ()
{
while(true) {
switch(state){
case State.Initialize:
Initialize();
break;
case State.Setup:
Setup();
break;
case State.Spawn:
Spawn();
break;
}
yield return 0;
}
// Instantiate(PlanetObject,new Vector3(-0.050f,0.15f,0.59f),Quaternion.identity);
}
private void Initialize()
{ Debug.Log(" We are in the Initialize function");
if(!checkForPlanetPrefabs())
return;
if(!checkForSpawnPoints())
return;
state = FDEnviroment.State.Setup;
}
private void Setup()
{ Debug.Log(" We are in the setup function");
state = FDEnviroment.State.Spawn;
}
private void Spawn()
{ Debug.Log(" We are in the spawn function");
GameObject[] ObjItem = AvailableSpawnPoints();
for(int cnt = 0; cnt < ObjItem.Length; cnt++)
{
GameObject it = Instantiate(PlanetObjectPrefab[Random.Range(0,PlanetObjectPrefab.Length)],ObjItem[cnt].transform.position,
Quaternion.identity)as GameObject;
it.transform.parent = ObjItem[cnt].transform;
}
state = FDEnviroment.State.Idle;
}
// check to see if we have at least one prefab t0 spawn
private bool checkForPlanetPrefabs()
{
if(PlanetObjectPrefab.Length > 0)
return true;
else
return false;
}
// check to see if we have at least one spawn point to spawn object
private bool checkForSpawnPoints()
{
if(PlanetSpawanLocation.Length > 0)
return true;
else
return false;
}
// Generate a list of available spawnpoints that do not have any objects childed to it
private GameObject[] AvailableSpawnPoints()
{
List ObjItem = new List ();
for(int cnt = 0; cnt ();
MyAccountChk = MyStats.Account;
if(MyAccountChk > 300)
{
PlanetDestroy();
}
}
}
Comment
I've reformatted your code. Be sure to use the little "1010101" button to format your code in the future.
What error messages you get? Where is your "PlanetDestroy" function?
So what is the problem? The only "Destroy" is this code is "PlanetDestroy()" (which you did not include the source for).
Without being able to look at the code that has the Destroy function, i can only guess that you are trying to destroy the prefab itself and not an instanced version of it. Ins$$anonymous$$d of destroying PlanetObject, you should store the gameobject Instantiate returns to a variable and destroy that:
var spawnedObject:GameObject;
spawnedObject=Instantiate(PlanetObject,new Vector3(-0.050f,0.15f,0.59f),Quaternion.identity);
Destroy(spawnedObject); //NOT Destroy(PlanetObject)