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
0
Question by HarleyK · Mar 22, 2017 at 07:15 AM · optimizationspawninggenerationobject pool

Object pulling prefabs that are randomly generating between random points?

Hello, originally i started using a generation script that is instantiating between a range of prefabs between an array of transforms. For a while it was working fine however i started using the profiler and realized this is causing a lot of problems with the cpu, it causing a lot of spikes when instantiating, and the trash manager building up. So i went into the object pooling live training video and created a basic object pooler. However, I'm still brain dead and mind boggled on how i could implement this strategy with the original script i have, and still keep it randomly spawning prefabs within the transform array. Any help or suggestions would be great. Here are the scripts.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Generation : MonoBehaviour {

 // These are the spawn point arrays that are being used as placement for what's being spawned
 public Transform[] terrainPoints;
 public Transform[] obstaclePoints;
 public Transform[] middlePoints;
 // These are the actual list of prefabs that will be spawning
 public GameObject[] terrainSpawn;
 public GameObject[] obstacleSpawn;
 public GameObject[] middleSpawn;
 // These are the time intervals in which the spawnings will occur
 public float terrainTimer = 2;
 public float obstacleTimer = 2;
 public float middleTimer = 2;
 public float newTime = 2;
 // This is the bool that determines whether the game will be pause (this is accessed by another script, not set by this one)
 public bool isPause;
 // This is accessing the level ring of the level, this isn't set in the inspector this is set dynamically
 public GameObject theGround;

 public static Generation S;

 void Awake()
 {
     S = this;
     theGround = GameObject.Find("level ring");
 }
 
 // Update is called once per frame
 void Update () {
     // If isPause is set to false then we proceed to spawn the clones using the custom functions
     if(isPause == false)
     {
         TerrainSpawn();
         ObstacleSpawn();
         MiddleSpawn();
     }
 }
 // This is the function thats being called thats doing the spawning for terrain prefabs
 void TerrainSpawn()
 {
     // This is taking the duration of time and decreasing it by a set frame
     terrainTimer -= Time.deltaTime;
     // If this is below or equal to zero, it spawns a new clone
     if(terrainTimer <= 0)
     {
         // This randomly chooses an indexed prefab and transform and spawns something at those points
         GameObject terrainClone = Instantiate(terrainSpawn[Random.Range(0, terrainSpawn.Length)], terrainPoints[Random.Range(0, terrainPoints.Length)].transform.position, transform.rotation);
         terrainClone.transform.parent = theGround.transform;
         // This resets the duration of time for the next spawn
         terrainTimer = newTime;
     }
 }
 // This is the function called to spawn the obstacle clones
 void ObstacleSpawn()
 {
     obstacleTimer -= Time.deltaTime;
     if(obstacleTimer <= 0)
     {
         GameObject obstacleClone = Instantiate(obstacleSpawn[Random.Range(0, obstacleSpawn.Length)], obstaclePoints[Random.Range(0, obstaclePoints.Length)].transform.position, transform.rotation);
         obstacleClone.transform.parent = theGround.transform;
         obstacleTimer = newTime;
     }
 }
 // This is the function being called to spawn clones for the middle section prefabs
 void MiddleSpawn()
 {
     middleTimer -= Time.deltaTime;
     if(middleTimer <= 0)
     {
         GameObject middleClone = Instantiate(middleSpawn[Random.Range(0, middleSpawn.Length)], middlePoints[Random.Range(0, middlePoints.Length)].transform.position, transform.rotation);
         middleClone.transform.parent = theGround.transform;
         middleTimer = newTime;
     }
 }

}

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 HarleyK · Mar 21, 2017 at 10:37 PM 0
Share

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class ObjectPoolerScript : $$anonymous$$onoBehaviour {

 public static ObjectPoolerScript current;
 public GameObject pooledObject;
 public int amountPooled = 20;
 public bool willGrow = true;

 List<GameObject> pooledObjects;

 // Use this for initialization
 void Start () {
     pooledObjects = new List<GameObject>();
     for(int i = 0; i < amountPooled; i++)
     {
         GameObject obj = (GameObject)Instantiate(pooledObject);
         obj.SetActive(false);
         pooledObjects.Add(obj);
     }
 }

 public GameObject GetPooledObject()
 {
     for(int i = 0; i < pooledObjects.Count; i++)
     {
         if (!pooledObjects[i].activeInHierarchy)
         {
             return pooledObjects[i];
         }
     }

     if (willGrow)
     {
         GameObject obj = (GameObject)Instantiate(pooledObject);
         pooledObjects.Add(obj);
         return obj;
     }

     return null;
 }
 
 // Update is called once per frame
 void Update () {
     
 }

}

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Sprite Spawning System with Array Help? 3 Answers

What is a good way to randomly generate clouds along a path? 0 Answers

New Object Pooling Problem 1 Answer

How do you efficiently target many instantiated prefabs for tweening? 0 Answers

When should I use object pool? 2 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