- Home /
How to make sure only one GameObject is instantiated at a time.
I want to instantiate an object but make sure that only one instance exist at all time. Example:
if (Input.GetMouseButtonDown(1))
{
Instantiate(gameObject);
}
But if you press down again the first gameObject gets deleted.
Answer by RaptorRush · Dec 05, 2020 at 02:52 AM
If you are not interested in implementing the optimization sacredgeometry suggested, and you prefer to destroy and create a new object each time, just save a reference to the last bullet you instantiated, so you can destroy it later. Like this:
private GameObject lastBullet;
private void Update() {
if (Input.GetMouseButtonDown(1)) {
if (lastBullet != null) Destroy(lastBullet);
lastBullet = Instantiate(bulletPrefab);
}
}
If the goal is a recreation of the same object, then would simply reusing the existing one be out of the question?
if(Input.Get$$anonymous$$ouseButtonDown(1))
{
if(lastBullet == null)
{
lastBullet = Instantiate(bulletPrefab);
}
else
{
// Do something with existing "lastBullet"
}
}
I'll think about that later when I rewrite the script. The main goal right now is just getting it to work.
Answer by sacredgeometry · Dec 04, 2020 at 09:46 PM
I wouldn't instantiate and delete it multiple times. There is a performance penalty when the GC kicks in to deallocate the memory you asked for.
I would just instead write a method to initialise the object. i.e. a method that just sets all that component/ objects properties to the state you want them set to.
Then I would just call/ invoke that method in the constructor or awake/ start monobehaviour method so that when you first create it it has all the right settings and then again any time you want it to be reset.
e.g.
public class Player : MonoBehaviour
{
public string Name;
public int Health;
void Init()
{
Name = "";
Health = 100;
transform.position = new Vector3(0, 0, 0);
}
void Start()
{
Init();
}
void Update()
{
if(Input.GetButtonDown("Jump"))
{
Init();
}
}
}
Not quite what I was looking for. I meant that if I press mouse1 then an object gets instantiated. If press again a new instance of the same object gets spawned and the previous one gets deleted so there can only be one instance of the object at a time.
Oh right. One second I will amend the answer.
Answer by CmdrZin · Dec 04, 2020 at 11:52 PM
You can do something like this.
int enemyCount = FindObjectsOfType<Enemy>().Length;
if(enemyCount == 0) {
// Spawn another
}
but the code it might be faster if you manage an enemy counter.
Increment on spawn, decrement on destroy.
Wait thats what I thought he meant but he said that it wasnt what he was looking for so edited my answer
I think I've been unclear. So I want to instantiate an object, a bullet from a gun, but I want it to be deleted every time I pull the trigger. I fire bullet 1 from a position. Bullet 1 does what it does. Then I press again. Bullet 2 spawns from the same position. Bullet 1 gets destroyed.
I read your answer as well and I already have assigned all the values that bullet is gonna have.
And as I said thats one of the worst things you can do in a managed language for adversely affecting your performance.
You should very rarely if ever be looking to destroy objects during your normal gameplay.
If you notice that if you start firing at any sort of rate your game starts to noticeably slow down that will be why.
ok, then use
GameObject[] objects = GameObject.FindGameObjectsWithTag("Bullet");
to get any existing Bullets. if objects.Length != 0, walk through the array in a for loop and Destroy(objects[i]).
Does your Bullet prefab have a tag named Bullet assigned?
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Instantiate problem 1 Answer
Destroying an Instantiated GameObject 4 Answers
Deleting a GameObject 2 Answers