prevent an Instantiated prefab to be re-Instantiate if it exist 2D Game in C#

42 views Asked by At

Hello and thanks for reading.

I'm not very experienced with coding and stuff, I have a shield that can be activated on collision

the problem if the shield is already Instantiated, and the player picks up again the shield power-up, the shield will double, and so on.

I want a solution for my Instantiation line below, to prevent the shield to be Instantiated many times every pickup.

thank you.

GameObject shi = Instantiate(bubble, bubblepoz.position, Quaternion.identity) as GameObject;
1

There are 1 answers

4
Prime On BEST ANSWER

Make "GameObject shi" a global variable by taking it out of the function.

Than do this

if (shi == null)
{
shi = Instantiate(bubble, bubblepoz.position, Quaternion.identity) as GameObject;
}