• 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
Question by TechTygr28 · Oct 11, 2022 at 06:30 AM · listspawning problems

Despawn Parts of Level After They Leave a Certain Range

I have experience with unity and c# but needed a little help with this one.

How can I despawn level parts after I have already used them during my game. I'm using a list to keep track of the spawned parts but I am having trouble keeping track deleting them once they have gotten out of a certain range from the player.

This is my level generator script so far.

Any help is appreciated. Thanks in advance.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LevelGenerator : MonoBehaviour {
 
     // Amount that checks is player is close enough to spawn the next platforms.
     private const float PLAYER_DISTANCE_SPAWN_LEVEL_PART = 200f;
     // Amount that checks is player is close enough to despawn the previous platforms.
     private const float PLAYER_DISTANCE_DELETE_LEVEL_PART = 200f;
 
     [Header("Level Technicalities")]
     [SerializeField] private Transform levelPart_Start;
     [SerializeField] private List<Transform> levelPartList;
     [SerializeField] private MobilePlayerMovement player;
     [SerializeField] private List<GameObject> levelListForDespawning;
 
     [Header("Positional Values / Offsets")]
     public float xPos = 0;
     public float yPos = -1;
     public float zPos = 35;

     private bool isStart;
     private Vector3 lastEndPosition;
     private Vector3 firstPlatformPosition;
     private bool isSpawningSimilarPart;
     int chosenLevelPartIndex;
     int lastLevelIndex;
 
 
     private void Awake() 
     {
         isSpawningSimilarPart = false;
 
         lastEndPosition = levelPart_Start.Find("EndPosition").position;
         firstPlatformPosition = levelPart_Start.Find("StartPlatform").position;
 
         int startingSpawnLevelParts = 5;
         isStart = true;
         for (int i = 0; i < startingSpawnLevelParts; i++) {
             SpawnLevelPart();
         }
     }
    
     private void Update() 
     {
         if (Vector3.Distance(player.transform.position, lastEndPosition) < PLAYER_DISTANCE_SPAWN_LEVEL_PART) 
         {
             SpawnLevelPart();
         }
 
         if (Vector3.Distance(player.transform.position, firstPlatformPosition) > PLAYER_DISTANCE_DELETE_LEVEL_PART) 
         {
            DespawnLevelPart(levelListForDespawning[0]);
         }
     }
 
     private void SpawnLevelPart() 
     {
         if(isStart == true)
         {
             chosenLevelPartIndex = 0;
             isStart = false;
         } else {
             do {
                 chosenLevelPartIndex = Random.Range(0, levelPartList.Count);
             }
             while(chosenLevelPartIndex == lastLevelIndex);
         }
  
         lastLevelIndex = chosenLevelPartIndex;
         Transform chosenLevelPart = levelPartList[chosenLevelPartIndex];
         Vector3 offset = new Vector3(xPos, yPos, zPos);
         /*
         if (chosenLevelPartIndex == 0) // The Tunnel
         {
             isSpawningSimilarPart = true;  
         }
         else if (chosenLevelPartIndex == 1) // Block-Box HARD
         {
             isSpawningSimilarPart = true;  
         }
         else if (chosenLevelPartIndex == 2) // Block-Box Easy
         {
             isSpawningSimilarPart = true;            
         } 
         else if (chosenLevelPartIndex == 3) // MiniMaze Left
         {
             isSpawningSimilarPart = false;
         }  
         else if (chosenLevelPartIndex == 4) // MiniMaze Right
         {
             isSpawningSimilarPart = true;
         } 
         else if (chosenLevelPartIndex == 5) // Slim Pathways
         {
             isSpawningSimilarPart = false;
         }*/
 
         Transform lastLevelPartTransform = SpawnLevelPart(chosenLevelPart, lastEndPosition + offset);
         levelListForDespawning.Add(lastLevelPartTransform.gameObject);
         lastEndPosition = lastLevelPartTransform.Find("EndPosition").position;
         firstPlatformPosition = levelListForDespawning[0].transform.position;
 
         lastLevelIndex = chosenLevelPartIndex;   
     }
 
 
     private Transform SpawnLevelPart(Transform levelPart, Vector3 spawnPosition) 
     {
         Transform levelPartTransform = Instantiate(levelPart, spawnPosition, Quaternion.identity);
         return levelPartTransform;
     }
 
 
     private void DespawnLevelPart(GameObject levelPartToDespawn)
     {
         //Destroy(levelPartToDespawn);
         Debug.Log(levelPartToDespawn);
         levelListForDespawning.Remove(levelPartToDespawn);
     }
 }
 
Comment

People who like this

0 Show 0
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

Answer by gregchristensen · Oct 11, 2022 at 06:56 AM

This official Unity tutorial includes distance check logic that might give you some inspiration:

How to work with multiple scenes in Unity - Distance Check

Comment

People who like this

0 Show 1 · 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 TechTygr28 · Oct 11, 2022 at 06:59 AM 0
Share

It didn't really help sorry. Any other ideas?

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.

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

148 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

Related Questions

A node in a childnode? 1 Answer

Spawn an object to a random spawn point from a list 2 Answers

Problem with dropping items from my inventory 0 Answers

How can I access the children of a gameobject using an index? 1 Answer

How to add item to Script Written in 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