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 mmadeira97 · Mar 19, 2019 at 05:34 AM · prefabprefab-instancepooling

PoolObject getting always random prefabs

Hey guys!

New guy in unity here, but i was following a tutorial of a endless runner, and my game is for android, and i want to use poolobjects because of performance, but i have two prefabs that i want to randomize, but again initially the script defines a pool of 5 random prefabs and stays that way the entire game, and my goal its to randomize always the prefabs... is there a way to do that?

Thank you!

public class Parallaxer : MonoBehaviour {

 class PoolObject {

     public Transform transform;
     public bool inUse;
     public PoolObject(Transform t) { transform = t; }
     public void Use() { inUse = true; }
     public void Dispose() { inUse = false; }
 }

 [System.Serializable]
 public struct YSpawnRange
 {
     public float min;
     public float max;
 }

 public GameObject[] Prefab;
 public int poolSize;
 public float shiftSpeed;
 public float spawnRate;

 public YSpawnRange ySpawnRange;
 public Vector3 defaultSpawnPos;
 public bool spawnImmediate;
 public Vector3 immediateSpawnPos;
 public Vector2 targetAspectRatio;

 float spawnTimer;
 float targetAspect;
 PoolObject[] poolObjects;
 GameManager game;

 void Awake()
 {
     Configure();
 }

 void Start()
 {
     game = GameManager.Instance;
 }

 void OnEnable()
 {
     GameManager.OnGameOverConfirmed += OnGameOverConfirmed;
 }

 void OnDisable()
 {
     GameManager.OnGameOverConfirmed -= OnGameOverConfirmed;
 }

 void OnGameOverConfirmed()
 {
     for (int i = 0; i < poolObjects.Length; i++)
     {
         poolObjects[i].Dispose();
         poolObjects[i].transform.position = Vector3.one * 1000;
     }
     if (spawnImmediate)
     {
         SpawnImmediate();
     }
 }

 void Update()
 {
     if (game.GameOver) return;

     Shift();
     spawnTimer += Time.deltaTime;
     if (spawnTimer > spawnRate)
     {
         Spawn();
         spawnTimer = 0;
     }
 }

 void Configure()
 {
     targetAspect = targetAspectRatio.x / targetAspectRatio.y;
     poolObjects = new PoolObject[poolSize];
     for (int i = 0; i < poolObjects.Length; i++)
     {
         var randomIndex = Random.Range(0, Prefab.Length);
         GameObject go = Instantiate(Prefab[randomIndex]) as GameObject;
         go.SetActive(true);
         Transform t = go.transform;
         t.SetParent(transform);
         t.position = Vector3.one * 1000;
         poolObjects[i] = new PoolObject(t);
     }

     if (spawnImmediate)
     {
         SpawnImmediate();
     }
 }

 void Spawn()
 {
     Transform t = GetPoolObject();
     if (t == null) return;
     Vector3 pos = Vector3.zero;
     pos.x = defaultSpawnPos.x;
     pos.y = Random.Range(ySpawnRange.min, ySpawnRange.max);
     t.position = pos;
 }

 void SpawnImmediate()
 {
     Transform t = GetPoolObject();
     if (t == null) return;
     Vector3 pos = Vector3.zero;
     pos.x = immediateSpawnPos.x;
     pos.y = Random.Range(ySpawnRange.min, ySpawnRange.max);
     t.position = pos;
     Spawn();
 }

 void Shift()
 {
     for (int i = 0; i < poolObjects.Length; i++)
     {
         poolObjects[i].transform.position += -Vector3.right * shiftSpeed * Time.deltaTime;
         CheckDisposeObject(poolObjects[i]);
     }
 }

 void CheckDisposeObject(PoolObject poolObject)
 {
     if (poolObject.transform.position.x < -defaultSpawnPos.x)
     {
         poolObject.Dispose();
         poolObject.transform.position = Vector3.one * 1000;
     }
 }

 Transform GetPoolObject()
 {
     for (int i = 0; i < poolObjects.Length; i++)
     {
         if (!poolObjects[i].inUse)
         {
             poolObjects[i].Use();
             return poolObjects[i].transform;
         }
     }
     return null;
 }

}

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 highpockets · Mar 19, 2019 at 06:54 AM 0
Share

You can use:

 Random.Range(0, poolObjects.Length);

And put it in GetPoolObject() method

Edit: If you have 2 pools, to randomize the 2 pools do the same as above for choosing which pool.. Random.Range(0,poolCount);

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Tsaras · Mar 19, 2019 at 07:11 AM

You could make 2 pools one for each object and have it randomize which pool to draw from. Otherwise it can get more complicated to solve if you maintain only one pool of all objects because you always search from the beginning of the pool.

EDIT: To change your paradigm to multiple pools, you can add a Pool class which will internally hold the gameobject instances of each pool like so:

 class PoolObject {
      public Transform transform;
      public bool inUse;
      public PoolObject(Transform t) { transform = t; }
      public void Use() { inUse = true; }
      public void Dispose() { inUse = false; }
  }
  
 class Pool {
     public PoolObject[] poolObjects;
     public GameObject prefab;
     public int poolSize;
 }

Then you can essentially set a prefab and poolsize for each pool and fill them up like so:

 public Pool[] pools;
 
 void Configure()
 {
     targetAspect = targetAspectRatio.x / targetAspectRatio.y;
     int poolCount = pools.Length;
     pools = new Pool[poolCount];
     for (int i = 0; i < poolCount; i++)
     {
         Pool currentPool = pools[i];
         currentPool.poolObjects = new PoolObject[currentPool.poolSize];
         for (int j = 0; j < currentPool.poolSize; j++)
         {
             GameObject go = Instantiate(currentPool.prefab) as GameObject;
             go.SetActive(true);
             Transform t = go.transform;
             t.SetParent(transform);
             t.position = Vector3.one * 1000;
             currentPool.poolObjects[j] = new PoolObject(t);
         }
         if (spawnImmediate)
         {
             SpawnImmediate();
         }
     }
 }

And also configure your GetPoolObject function to take a pool id parameter:

 Transform GetPoolObject(int poolID)
 {
     Pool whichPool = pools[poolID];
     int poolObjectCount = whichPool.poolObjects.Length;
     for (int i = 0; i < poolObjectCount; i++)
     {
         PoolObject poolObject = whichPool.poolObjects[i];
         if (!poolObject.inUse)
         {
             poolObject.Use();
             return poolObject.transform;
         }
     }
     return null;
 }

And finally call GetPoolObject with a random int:

 var randomIndex = Random.Range(0, pools.Length);
 Transform t = GetPoolObject(randomIndex);
Comment
Add comment · Show 3 · 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 mmadeira97 · Mar 19, 2019 at 09:54 AM 0
Share

Good idea really! I can use that on GetPoolObject and with that i randomize what poolobject to get and done, i think

avatar image mmadeira97 · Mar 19, 2019 at 11:08 AM 0
Share

@Tsaras But can you give me a hint how to work around it?

avatar image Tsaras mmadeira97 · Mar 23, 2019 at 07:59 AM 0
Share

Sorry for the delay I edited my answer above.

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

131 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

Related Questions

Prefabs spawn with wrong values 0 Answers

Possible to make "override all" button on prefab instances ignore certain values (like it does for position)? 0 Answers

How to use pool to spawn object instead of instantiate? 2 Answers

Using Resources.load with a variable 1 Answer

Link GUIText to a prefab? 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