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 /
  • Help Room /
avatar image
0
Question by Lukephos · Apr 24, 2017 at 07:05 PM · c#instantiatelistdestroy

Instantiate and then destroy a prefab in a list. C#

I'm trying to spawn a simple object with some kill code in it that will be destroyed in a collision event later on. I'm not too sure on what I'm doing but as far as I can tell I need to make it into a prefab then instantiate it into a list and then destroy it in the collision and clear the list. The list is necessary since there are multiple of them and I only want to remove some, not to mention them all having the same name making it confusing. I've been trying for a while and think I've been able to get it to go into the list but it only seems to ever allow one in there and I could only make it go as a Object rather than GameObject. I cant destroy it since it's saying something about it only trying to destroy the transform and I can't figure it out. Here is the code for the object spawner:

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class ObstacleGenerator : MonoBehaviour {

 public Object Obstacle;
 private float yPos;
 public List<Object> currentObstacles = new List<Object>();

 public void Start () {
     yPos = Random.Range (20, 200);
     currentObstacles.Add (Instantiate (Obstacle, new Vector3 (250f, 1.5f, yPos), Quaternion.identity));

And here is the code for the collision:

public class ResetPos : MonoBehaviour {

 public ObstacleGenerator obstacleGeneratorScript;

 void OnTriggerEnter(Collider Player){

     obstacleGeneratorScript.GetComponents<ObstacleGenerator> ();
     Destroy(obstacleGeneratorScript.currentObstacles[0]);
     obstacleGeneratorScript.currentObstacles.Clear();
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

1 Reply

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

Answer by PizzaPie · Apr 25, 2017 at 12:45 PM

You are right till the "...make it into a prefab then instantiate" the whole list thing is useless unless you need to refer to them later on. Now for the destroy event which is triggered by a collision you need to attach that script on the prefab and make the collider of the prefab a trigger , assuming you want it to be destroyed when collide with the Player GameObject.

 void OnTriggerEnter(Collider col)
     {
         if (col.CompareTag("Player"))
         {
             Destroy(gameObject);
         }
     }

Make sure to add the tag Player on the player GO.

And this to Instatiate:

 public GameObject obstaclePrefab;
     void Start()                //don't use the Start though because you ll only instatiate one obstacle!
     {
         float yPos = Random.Range(20, 200);
         Instantiate(obstaclePrefab, new Vector3(250f, 1.5f, yPos), Quaternion.identity));
     }


As for the fact that you list is taking only objects is because you "instructed" it to do so if you change the List to List and the Object Obstacle to GameObject Obstacle. There is no point to use Object in this case as it is a base class and won't hold lot of info you need stick to GameObject instead. Cheers.

Note: if you instatiate and destroy the same prefab(object) again and again i highly recommend to create a pooling system and just disable/enable them accordingly, as on how to do this, look around there are several tutorials on the subject.

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

345 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 do I destroy a GameObject/Script that instantiates other objects, without destroying the objects it has previously instantiated? 0 Answers

Destroy prefabs after they have been instantiated (C#) 1 Answer

Destroy GameObject in list then instantiate GameObject not working 1 Answer

Unable to destroy instantiated prefab during OnMouseExit,Unable to Destroy previously instantiated object OnMouseExit() 0 Answers

How to destroy on exiting PlayMode/EditMode? 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