Grow Animation Grow Animation - 18 days ago 11
C# Question

Destroy prefab instance

I am implementing a smoke prefab in my game. The idea is to create 3 different instances of the prefab (in different positions) then destroy them according to a toggle button. The problem the instances are not being destroyed and I have no idea why.

This is my script:

class Smoke1 : MonoBehaviour
{
public GameObject myPrefab;
public GameObject canvasObject;

public static GameObject newSmoke1;
public static GameObject newSmoke2;
public static GameObject newSmoke3;

public int toggle1;
public int toggle2;
public int toggle3;

public Vector3 smokeposition1 = new Vector3 (397, -394, 90);
public Vector3 smokeposition2 = new Vector3(414, -402, 90);
public Vector3 smokeposition3 = new Vector3(432, -410, 90);

string newSmoke;


void Start()
{
i = 1;
toggle1 = 2;
toggle2 = 2;
toggle3 = 2;
newinstance(smokeposition1, newSmoke1);
newinstance(smokeposition2, newSmoke2);
newinstance(smokeposition3, newSmoke3);
}

void Update()
{

togglecheck(ThermoElectric.t1Bool, toggle1, newSmoke1, smokeposition1);
togglecheck(ThermoElectric.t2Bool, toggle2, newSmoke2, smokeposition2);
togglecheck(ThermoElectric.t3Bool, toggle3, newSmoke3, smokeposition3);
}

void newinstance(Vector3 smokeposition, GameObject smokeinstance)
{
smokeinstance = Instantiate(myPrefab, smokeposition, Quaternion.Euler(-90, 0, 0)) as GameObject;
smokeinstance.transform.SetParent(canvasObject.transform, false);
smokeinstance.transform.localScale = new Vector3(1, 1, 1);

smokeinstance.GetComponent<ParticleSystem>().startColor = new Color(0.1f, 0.1f, 0.1f, 1);

}

void togglecheck(bool turbine, int togglei, GameObject smokeinstancet, Vector3 smokeposition)
{

if (turbine == true && togglei == 1)
{
newinstance(smokeposition, smokeinstancet);
togglei = 2;
}

if (turbine == false && togglei == 2)
{
Destroy(smokeinstancet, 0.1f);
togglei = 1;
}
}
}


The bools are working and the if conditions are met, but the prefabs don't get destroyed..Can you help?

Answer

The bools are working and the if conditions are met, but the prefabs don't get destroyed..Can you help?

1.You shouldn't be destroying prefabs. You should only destroy the GameObject created with Instantiate.

2.Remove the statics from the GameObject. I am sure you don't need that.

If you are %100 sure sure that the if conditions are met then the newinstance function is the problem.If you are not sure, put Debug.Log inside if (turbine == false && togglei == 2) to verify that this condition is met because without that, Destroy(smokeinstancet, 0.1f); wont be called.

Problem with the newinstance function.

void newinstance(Vector3 smokeposition, GameObject smokeinstance)
{
    smokeinstance = Instantiate(myPrefab, smokeposition, Quaternion.Euler(-90, 0, 0)) as GameObject;
    smokeinstance.transform.SetParent(canvasObject.transform, false);
    smokeinstance.transform.localScale = new Vector3(1, 1, 1);

    smokeinstance.GetComponent<ParticleSystem>().startColor = new Color(0.1f, 0.1f, 0.1f, 1);
}

The smokeinstance = Instantiate(myPrefab, smokeposition, Quaternion.Euler(-90, 0, 0)) as GameObject; code will assign the instantiated GameObject to the local variable in the parameter called smokeinstance. It will not assign it to the GameObjects passed into it such as newSmoke1,newSmoke2 and newSmoke3.

newinstance(smokeposition1, newSmoke1);
newinstance(smokeposition2, newSmoke2);
newinstance(smokeposition3, newSmoke3);

SOLUTION:

You have 2 solutions:

1.Use the ref or out keyword. In this case, we will just use the ref keyword.

void newinstance(Vector3 smokeposition, ref GameObject smokeinstance)
{
 ......
}

then also use ref while calling it.

newinstance(smokeposition1, ref newSmoke1);
newinstance(smokeposition2, ref newSmoke2);
newinstance(smokeposition3, ref newSmoke3);

2.Make newinstance function return GameObject instead of void.

GameObject newinstance(Vector3 smokeposition)
{
    GameObject smokeinstance = Instantiate(myPrefab, smokeposition, Quaternion.Euler(-90, 0, 0));
}

then call it:

newSmoke1 = newinstance(smokeposition1);
newSmoke2 = newinstance(smokeposition2);
newSmoke3 = newinstance(smokeposition3);

Any one of this should solve your problem.