Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Maypher · Jan 17, 2021 at 01:13 AM · instantiate2d gamedestroyinfinite runner

How to handle Instantiation and Destroying on an infinite runner?

I'm working on an infinite runner where you control a car and have to dodge other vehicles coming your way. I know, really creative right?

I've got the movement already figured out, my problem comes when trying to spawn the cars that act as obstacles. I've made a simple script that spawns a car and removes it when it's outside of the player's view.

     public GameObject[] cars; //List of all car prefabs
     private Vector3[] spawnLocations = {new Vector3(-7.45f, 8, 0), new Vector3(-4, 8, 0), new Vector3(0, 8, 0), new Vector3(4.5f, 8, 0), new Vector3(7.5f, 8, 0)};
 
     [Header("Time manager")]
     public float spawnTime; //Interval between spawns
     private float currentTime = 0;
 
 
     private GameObject currentClone;
     private bool spawned;
 
     [Header("Cars movement")]
     public float moveSpeed;
     private float DestroyLocation = -6.4f;
 
     // Update is called once per frame
     void Update()
     {
         currentTime += Time.deltaTime;
         if (currentTime >= spawnTime && !spawned)
         {
             currentTime = 0;
             currentClone = CarSpawn(cars, spawnLocation); //Function to spawn.Not required for example
             spawned = true;
         }
         else if (spawned)
         {
             currentClone.GetComponent<Rigidbody2D>().velocity = new Vector2(0, moveSpeed);
             if(currentClone.transform.position.y <= DestroyLocation)
             {
                 DeleteCar(currentClone); //Function to Destroy. Not required for example
                 spawned = false;
             }
         }
     }

This kind of works. It does spawn a car and destroys it when no longer needed. The problem comes from the fact that I can only spawn 1 car at a time. How can I rewrite the code to handle multiple cars on screen

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 Aisenhein · Jan 17, 2021 at 01:26 AM

Hello, I think with a script in the car is better, so the car it self will self destroy when go of-screen or something, so your manager just need to spawn as many cars (or objects) you want

Other point, a better approach for your case I think is to use a object pool instead of instantiate every car every time, basically with this structure of object pool, you instantiate all cars (or other objects) at the begin when load the scene, and just use and reuse then https://learn.unity.com/tutorial/introduction-to-object-pooling

Comment
Add comment · 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 Maypher · Jan 17, 2021 at 01:55 AM 0
Share

Thanks! The first option worked just fine. I might try to use object pool later for optimization but for now I can continue just fine.

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

168 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

Related Questions

How to destroy a object when it's off screen and re-instantiate it when it gets back on screen 0 Answers

Cannot destroy instantiated objects 2 Answers

Is it possible to create an instance of a prefab without instantiating it into the scene? 1 Answer

Mouse Methods (OnMouseDown, OnMouseOver etc) stop working after certain circumstances 0 Answers

Extra objects keep appearing in scene view 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