Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 mrinalmaurya1 · Feb 28 at 03:59 PM · destroydestroy objectprefab-instance

Need assistance with destroying Gameobjects

I am making a endless runner type game, having problem with destroying platforms once its off my screen. Just want to clean up my hierarchy panel. Here is my platform script....

public Transform PlatformPrefab; private Vector3 _nextTileSpawn; void Start() { _nextTileSpawn.z = 12; StartCoroutine(SpawnPlatform()); }

void Update() {

 }
 IEnumerator SpawnPlatform()
     {
         yield return new WaitForSeconds(0.5f);
         Instantiate(PlatformPrefab, _nextTileSpawn, PlatformPrefab.rotation);
         _nextTileSpawn.z += 4;            
     Instantiate(PlatformPrefab, _nextTileSpawn, PlatformPrefab.rotation);
         _nextTileSpawn.z += 4;          
         StartCoroutine(SpawnPlatform());
     }

}

Comment
Add comment · Show 1
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 metalted · Feb 28 at 06:55 PM 0
Share

I noticed that you are not actually destroying the game objects. What have you tried ? And why are you running the same 2 lines of code twice ? I can show you an example of how to write a simple script to do this, and you don't really need to use Coroutines. I feel a lot of people use them when starting out because it has the function WaitForSeconds(...), which is what most people want. The same can be achieved without Coroutines.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by metalted · Feb 28 at 07:03 PM

This below is untested code, but i hope it gives you an idea of how to handle the problem. Instead of a coroutine i just used a timer to simplify things. The timer will run and each time the timer runs out we create a platform. When creating a platform we save a reference to it, so we are able to destroy it later. At the moment I used an arbitrary amount of maximum platforms. I hope the comments will help you out with anything that isn't clear. The most important part of this, is to save a reference to your created objects (in an array or list...), so you can access them later.

 public Transform platformPrefab;
 public Vector3 nextTileSpawn;
 
 //A list to hold all the instantiated platforms.
 public List<GameObject> activePlatforms = new List<GameObject>();
 //How many platforms can be active at once?
 public int maxPlatforms = 3;
 
 //Timer
 public float timerValue = 0.0f;
 public float timerMax = 0.5f;
 
 public void Start()
 {
     //Some conditions here...
     gameRunning = true;
 }
 
 
 public void Update()
 {
     //Increment the timer
     timerValue += Time.deltaTime;
     
     //Has the timer expired?
     if(timerValue >= timerMax)
     {
         //Reset the timer
         timerValue = 0;
         CreatePlatform();
     }
 }
 
 public void CreatePlatform()
 {
     //First create a new platform and add it to the list.
     activePlatforms.Add(Instantiate(platformPrefab, nextTileSpawn, Quaternion.identity));
     //Increment the position for the next tile.
     nextTileSpawn.z += 4;
     //Check how many tiles are active now, if more than allowed, remove the oldest one.
     if(activePlatforms.Count > maxPlatforms)
     {
         //To many platforms, remove the first one.
         GameObject p = activePlatforms[0];
         //Remove from the list.
         activePlatforms.RemoveAt(0);
         //Destroy the object.
         Destroy(p);
     }
     
 }



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

137 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

Related Questions

Script doesn't work anymore! 2 Answers

Hi Im just trying to get my projectile to destroy a game object and Im having a hard time figuring it out. Any help is appreciated.c# :) 1 Answer

Destroying a Button when you click it. 1 Answer

Why are the objects (they are cows) randomly disappearing? 2 Answers

How to play a destroy animation? Brick Breaker/Arkanoid. 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