Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Grorg · Apr 28, 2017 at 08:34 AM · scriptingbasicsgame object

beginner question: behavior of duplicated objects

Im new to unity, and trying to figure out the best way to work out a game mechanic. I have a game object that is duplicated around 100 times. The longer the user performs an action i.e. push the f key, the more the objects will disappear until they are all gone. As the objects are named like object-1, object-2, etc. it seems to me that there should be someway that I just increment a counter and have the same script run to remove each object. Is this correct and if so what do I attach the script to or does it need to be attached to each individual object?

Comment
Add comment · Show 4
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 EdwinChua · Apr 28, 2017 at 08:44 AM 0
Share

@Grorg I would suggest that you create a Prefab, and call Instantiate(Prefab, parent) to create your objects. At the same time, you add them to a list. When you need to destroy them, simply call destroy, and remove it from the list.

 public GameObject prefab; //assign this in the inspector
 List<GameObject> listOfGameObjects;
 
 void OnStart()
 {
 listOfGameObjects = new List<GameObjects>();
     for (int i =0; i < 100; i++)
     {
         GameObject temp = Instantiate(prefab, transform); //assu$$anonymous$$g you've attached this 
         //script to the parent
         listOfGameObjects .Add(temp);
     }
 }
 
 public void DestroyObject() // Assign to 'f' press event
 {
     GameObject temp = listOfGameObjects [listOfGameObjects.Count-1];
     Destroy(listOfGameObjects [listOfGameObjects.Count-1]); // removes the last element 
     // added
     listOfGameObjects.Remove(temp );
 }
 
avatar image Masterio · Apr 28, 2017 at 10:46 AM 0
Share

You can make one script on separeted GameObject lets say "Controller". Next when you click F then you can randomly destroy the object [objects]. $$anonymous$$eep all objects in an array.

avatar image Grorg · May 03, 2017 at 02:37 AM 0
Share

Thanks! Had to do a couple of small changes, but it was a big help. I will post my code once I have it a little further refined.

avatar image EdwinChua Grorg · May 03, 2017 at 02:58 AM 0
Share

@Grorg You're welcome! Do note that Instantiate() and Destroy() are resource intensive, so if you intend to hide and display the object repeatedly, you'd be better off using gameObject.SetActive(true/false) or moving the object out of sight (eg. below the game plane).

I made the mistake of trying to repeatedly destroy and re-instantiate several hundred objects every second, which totally hung the game. Had to do quite a bit of re-coding to correct the problem.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Igor_Vasiak · Apr 28, 2017 at 12:09 PM

Create a script and type this at OnMouseStay loop (it doesn't matter if it's C# or JavaScript, it will work for both):

  if (Input.GetKeyDown(KeyCode.F)) {
       Destroy(gameObject);
  }

Then select all of your objects by typing on the search label at the Hierarchy window: object, then select the first one, scroll down, hold SHIFT and select the last one and drag your script at the Inspector window.

Note: Just know that, if you drag one of those objects to the project tab you'll have a prefab with all of the attributes you've placed at the object. Then all you'll need to do is drag ONE object prefab to the Hierarchy and it doesn't matter how much copies of the prefab you have, each modification you make on it will be applied to each one of them. Hope I answered your question.

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

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

7 People are following this question.

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

Related Questions

need particle help 1 Answer

Can someone explain 'Using ..." and "MonoBehaviour" in C# 7 Answers

Unity Scripting armv7 Error 1 Answer

Good or Bad ending 1 Answer

Script that switches between first and third person controller 3 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