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
1
Question by GamingNewBie · Jan 20, 2013 at 10:14 AM · prefabsloadingspawning

Spawning Coins infront of the player

I want to spawn coins in front of the players path (Right, left). Any ideas how should i do it? I am making a chunks of coins and spawn them when the player reaches a certain point, When ever the coins(chunk) are spawned, i feel a jitter on the screen like a minor halt while loading coins. Please also advice on instantiating prefabs on run time. What is the best possible way to do that, so that it wont compromise the game play. I am new to unity, just point me to the right direction. Thanks

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

2 Replies

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

Answer by aldonaletto · Jan 20, 2013 at 01:25 PM

Instantiate is a heavy operation: it allocates and initializes lots of memory blocks for each object created. When the object is destroyed, these blocks become useless, and at certain times another heavy operation called "garbage collection" must be executed by the system to make this memory available again.
In order to reduce these performance killer operations, you can create an "object pool" for the coins: it's a list that keeps the coin objects; instead of destroying a coin, deactivate it; instead of creating a new coin, re-activate one of the deactivated coins at the desired position/rotation.
An object pool can be easily implemented using List, like below:

 import System.Collections.Generic;
 
 var coinPrefab: GameObject;
 var coinPool = new List.<GameObject>();
 
 function IsCoinFree(coin: GameObject): boolean {
   return !coin.activeInHierarchy;
 }

 // use this instead of Instantiate:
 function InstantiateCoin(position: Vector3, rotation: Quaternion): GameObject {
   var coin: GameObject = coinPool.Find(IsCoinFree); // is any free coin in the pool?
   if (coin){ // yes, this coin is free:
     coin.SetActive(true); // activate it...
     coin.transform.position = position; // at the desired position...
     coin.transform.rotation = rotation; // and orientation
   } else { // no, all coins are in use:
     // instantiate a new coin:
     coin = Instantiate(coinPrefab, position, rotation);
     coinPool.Add(coin); // and add it to the pool
   }
   return coin; // anyway, return the coin GameObject reference
 }

 // always use this to destroy the coin:
 function DestroyCoin(coin: GameObject){
   // just deactivate it - the coin now is free in the pool
   coin.SetActive(false);
 }

The idea is simple: when you want to create a new coin, call InstantiateCoin(position, rotation): this will first search in the pool for free coins; if there's a free coin (deactivated) in the pool, activate it at the desired position/rotation and return a GameObject reference to it; if there's none, create a new one as usual, add it to the pool and return its reference. When you don't need the coin anymore, "destroy" it using DestroyCoin(coin): the coin is simply deactivated, so that it disappears from the scene and become available in the pool (deactivated objects in the pool are considered available).
NOTE: If the coins have their own scripts, you may have to call a custom Start function to initialize their variables, so that a recycled coin behaves exactly like a brand new one.

Comment
Add comment · Show 4 · 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 GamingNewBie · Jan 20, 2013 at 01:52 PM 0
Share

Thank you so much for the help.

avatar image AlucardJay · Jan 22, 2013 at 03:34 AM 0
Share

Don't forget to accept this excellent answer =]

avatar image antk · Jan 02, 2014 at 04:42 AM 0
Share

Yeah, the OP should accept this answer. Really helped me out.

avatar image raf_321 · Dec 28, 2014 at 09:18 PM 0
Share

Do I add this to an empty game object / the player / coin?

avatar image
0

Answer by cdrandin · Jan 20, 2013 at 11:09 AM

Assuming these coins of a sphere collider. Give some distance away from the player. Assuming the player is facing the z axis, standing vertical (y axis). So we want to spawn these coins along the x axis. Have some set distance, so, distanceFromPlayer which is on the z axis. So just give how long you want it to go for lets display 4. So divide that in half and take the ceiling, for uneven number of coins. Now just place them along as such Vector3(transform.position.x + distanceFromPlayer, transform.position.y, transform.position.z + location)

location depends on the radius of sphere collider. So, if the coin is 4 units longs. Then take increments of 4 going along the path, placing them neatly in front of the player. Obviously, right now it places it in 1 place, just modify it to make multiple ones and just move along side the x axis. You could also use Vector3.Lerp to have it move smoothly.

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 GamingNewBie · Jan 20, 2013 at 01:28 PM 0
Share

$$anonymous$$ore clearer picture of the problem

There are three lanes in which the character can run. player run straight towards Z-axes. On turns it runs towards X axis. Coins should be placed like bread crumbs, like a trail of coins in front of the player in advance.(like temple run, Epic knight)

I like the idea u shared but its most appropriate for spawning coins on random intervals. Unfortunately my problem is little different. Any help would be highly appreciated.

Thanks for giving the time & thought.

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

14 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

Related Questions

How can I spawn a series of objects along a path? 3 Answers

Spawning Player After Player Choice. 1 Answer

Spawning in a specified location 2 Answers

How do I make enemies not spawn in the same position? 2 Answers

Are sprites reference (script) loaded every time? 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