Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mrroundtree22 · Dec 04, 2020 at 09:37 PM · gameobjectinstantiateinputdestroy

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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);
         }
     }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mrroundtree22 · Dec 05, 2020 at 03:13 AM 0
Share

Yes, it works, thank you.

avatar image Eno-Khaon · Dec 05, 2020 at 06:25 AM 0
Share

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"
     }
 }
avatar image Mrroundtree22 Eno-Khaon · Dec 05, 2020 at 10:58 AM 0
Share

I'll think about that later when I rewrite the script. The main goal right now is just getting it to work.

avatar image
1

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();
         }
     }
 }
 


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mrroundtree22 · Dec 04, 2020 at 10:02 PM 0
Share

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.

avatar image sacredgeometry Mrroundtree22 · Dec 04, 2020 at 10:14 PM 0
Share

Oh right. One second I will amend the answer.

avatar image
0

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.
 
 

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sacredgeometry · Dec 05, 2020 at 12:37 AM 0
Share

Wait thats what I thought he meant but he said that it wasnt what he was looking for so edited my answer

avatar image Mrroundtree22 sacredgeometry · Dec 05, 2020 at 12:52 AM 0
Share

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.

avatar image sacredgeometry Mrroundtree22 · Dec 05, 2020 at 12:52 PM 0
Share

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.

avatar image CmdrZin · Dec 05, 2020 at 01:40 AM 0
Share

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]).

avatar image Mrroundtree22 CmdrZin · Dec 05, 2020 at 01:54 AM 0
Share

Couldn't get it to work.

avatar image CmdrZin Mrroundtree22 · Dec 05, 2020 at 02:21 AM 0
Share

Does your Bullet prefab have a tag named Bullet assigned?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

267 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

using destroy with an array of GameObjects (C#) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges