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
2
Question by CloudiaNBusiness · Sep 09, 2017 at 10:30 AM · generationlaggygenerateinfinite runner

Remove old Objects in scene

Okay. I'm making an infinit runner. But I can't figure out how to remove objects that I've run past. I am very sorry if my code is bad. This is my first game. I need a no-lag solution. :)

IF you want to see what the game looks like: alt text

HERE IS THE CODE:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class generation : MonoBehaviour {
 
     
     private float speed = 5f;
     public static float groundNumber = 10;
     private float passedNumber = 0f;
     GameObject startPlatform;
     GameObject gro

     public float id = 0f;
 
 
     //PREFABS FOR INSTANSIATE
     public static GameObject wallPrefab;
     public static float genNumber;
     public static GameObject groundPrefab;
     
    
     // Use this for initialization
     
     
     void Start () {
 
         startPlatform = GameObject.Find("StartingPlatform");
         wallPrefab = GameObject.Find("Wall");
         groundPrefab = GameObject.Find("StartingPlatform");
         gameObject.GetComponent<playerController>();
         wallPrefab.GetComponent<Renderer>().material.color = Color.green;
         groundPrefab.GetComponent<Renderer>().material.color = Color.white;
         groundNumber = 10;
         neverRepeat = false;
         neverRepeat2 = false;
         neverRepeat3 = false;
         neverRepeat4 = false;
 
     }
 
 
     public static bool neverRepeat = false;
     public static bool neverRepeat2 = false;
     public static bool neverRepeat3 = false;
     public static bool neverRepeat4 = false;
     private int wall = 0;
     // Update is called once per frame
     void DestroyPlatform ()
     {
 
     }
     void FixedUpdate () {
 
         #region Ground
 
         GameObject ground = GameObject.CreatePrimitive(PrimitiveType.Cube);
         ground.transform.position = new Vector3(0, -1, 0);
         ground.transform.localScale = new Vector3(10, 1, 10);
             ground.tag = "platformNumber";
         
         
         ground.transform.position = new Vector3(0, -1, groundNumber);
         ground.GetInstanceID();
 
        
         groundNumber += 10;
         
         if (groundNumber > 1000 && neverRepeat == false)
         {  
             Debug.Log("Ground Number is more than 1000");
             neverRepeat = true;
             playerController.movementSpeed += 2f;
             id = +1;
 
         }
 
         if (groundNumber > 3000 && neverRepeat2 == false)
         {
             Debug.Log("Ground Number is more than 3000");
             neverRepeat2 = true;
             playerController.sideMoveSpeed += 0.01f;
             playerController.movementSpeed += 2f;
         }
         if (groundNumber > 5000 && neverRepeat3 == false)
         {
             Debug.Log("Ground Number is more than 5000");
             neverRepeat3 = true;
             playerController.movementSpeed += 1f;
         }
         if (groundNumber > 7000 && neverRepeat4 == false)
         {
             Debug.Log("Ground Number is more than 7000");
             neverRepeat4 = true;
             playerController.sideMoveSpeed += 0.01f;
             playerController.movementSpeed += 1f;
         }
 
         #endregion
         
         
         Vector3 wallPOS = new Vector3(Random.Range(-5f, 5f), 0, Random.Range(-5f, 5f) + id);
         Instantiate(wallPrefab, wallPOS, Quaternion.identity);
         id += 10;
 
 
 
 
     }
 
 }


skarmklipp.png (118.1 kB)
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
1

Answer by tormentoarmagedoom · Sep 09, 2017 at 11:25 AM

Good day @CloudiaNBusiness .

I did not read your code, i will just give you a solution for your problem. As i understand, you have a camera that is moving forward over a platform, and you want to destroy everything behind the camera because you will never see it again.

Than what i recommend you is to create a empty GameObject children & behind the camera. This way, this object will move with the camera. Lets call this object "Destroyer".

Then add a BoxCollider component to Destroyer, and activate it "Trigger" option. Make this Collider big enough to collide with all objects that are in the platform, including the platform. As it will move with the camera, all objects once get out of camera vision, have to collide with the Destroyer.

We will make a script that destroys all the GameObjects that touches the Destroyer. For do this we will use the "OnTriggerEnter" wich will be called when some other collider touches the Destroyer collider. So, All GameObjects that you want to be destroyed, must have a BoxCollider!

So, the Destroyer must have this code in one of its scripts:

 private void OnTriggerEnter (Collider ColliderToBeDestroyed)
 {
 Destroy(ColliderToBeDestroyed.gameObject)
 }

Note that "ColliderToBeDestroyed" is a Collider component, so you have to refer to the GameObject that contains that collider that has touched the Destroyer

Well, i think i said all you need to do this. I recommend you to make segments of the platform to be able to destroy the platform either.

If need something else, just ask using @tormentoarmagedoom Bye:D

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

69 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

Related Questions

This code crashes unity, please help! 0 Answers

Runtime mesh generation from text file 0 Answers

Minecraft (Cube) Terrain 1 Answer

Endless Runner world generation stops at certain point. 2 Answers

Problem with creating a random generation script 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