• 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 /
This question was closed Mar 12, 2020 at 06:47 PM by mjonasz for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by mjonasz · Mar 10, 2020 at 06:55 PM · instantiate prefab

How to create multiple gameObjects?

I am working on an editor for a game where the player can create their own scenario. This editor allows them to purchase enemy gameObject of different types. Doing this is not a problem - I have a list of buttons, each of which lets the player buy a gameObject of "unitType". But I have a problem with instantiating these units. I have considered 2 options: 1 - every time the player buys a gameObject, this gameObject (a prefab) gets instantiated. But what if the player changes their mind and decides to get rid of that gameObject and buy another one instead (this is still on the purchasing screen)? I suppose I would look for a gameObject with a specific "unitType" and destroy it - but how do I destroy only a single one, instead of all the gameObjects with that "unitType"? 2 - the gameObjects get instantiated only after all the purchasing is done. In that case, how do I instantiate the number of gameObjects of a specific "unitType" that the player has purchased (int inventory)?

Comment
Add comment · Show 3
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 tormentoarmagedoom · Mar 10, 2020 at 07:00 PM -1
Share

Hello.

OR i dont understand your question, or your question is too simple.. how do I destroy only a single one? How you prettend to destroy the object?

Destroy (GameObject);

Where is the problem? have a reference to that object, that all.

avatar image mjonasz tormentoarmagedoom · Mar 10, 2020 at 07:07 PM 0
Share

For example, the player purchases 10 objects of "unitType = 1" and these get instantiated when still in the purchasing screen. Then the player decides that they want only 9 objects of "unitType = 1", so they press a "$$anonymous$$us" button. This reduces the number of "unitType = 1" objects in their inventory to 9 but I don't know how to destroy just one of the 10 "unitType = 1" prefabs that had already been instantiated.

avatar image JPhilipp · Mar 10, 2020 at 08:50 PM 1
Share

Try keep track of the temporarily-spawned objects in a list. $$anonymous$$aintain that list based on the user menu interaction (e.g. "-" of a type removes the last one of the last, while also destroying its gameObject reference).

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by mjonasz · Mar 12, 2020 at 11:12 AM

The solution was rather simple. When the player pressed the "-" button, a list of objects with the unitType corresponding to that button was created and object[0] in that list was destroyed. Then the list was cleared.

Comment
Add comment · 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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

123 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

Related Questions

How to replace prefab at the same location? 1 Answer

Instantiated Object does not fall 3 Answers

Instantiate Prefab Gameobject vs Transform 2 Answers

Object not Instantiating Correctly 1 Answer

Missing Prefab DontDestroyOnload.,Can´t instantiate dontDestroyOnLoad prefab in other scenes. 0 Answers


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