- Home /
Destroy Prefab in programming
I created GameObject and instantiated then destroyed. I gave that Powerups () function in Update. So, After the destruction of the GameObject the IF condition is checking still what could i do to solve this to call that Game Object whenever needed
Note: I didn't use physics and all.
void Powerups()
{
CreatePowerUp();
if((transform.position.x + (widthPlayer/2)) >= (Powup.transform.position.x))
{
Debug.Log("Objct");
Destroy(Powup);
}
}
void CreatePowerUp()
{
if(!val1)
{
Powup = Instantiate(Powerup) as GameObject;
val1 =true;
}
}
By the way I see, the "if" will be checked wherever you create a new powerup. What we have here is a powerup "factory", not the powerup object itself.
Answer by Sonaten · Nov 06, 2012 at 01:33 PM
An easier way to handle it could be to put a script on the Powup Prefab.
But in your case. Maybe check if there is a Powup?
if(Powup != null){
if((transform.position.x + (widthPlayer/2)) >= (Powup.transform.position.x))
{
Debug.Log("Objct");
Destroy(Powup);
}
}
In your scenario you might end up with more than one powerup in the level (I say this on an assumption). And every time you spawn a new one, Powup will not point to the old GameObject fabrication. You might want to look at Lists in C# to handle multiple gameobjects.
Enjoy