You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity球类游戏:多预制体实例化失败问题求助

Fixing Multi-Prefab Object Pool for Your Column Spawner

Let's get this sorted out—your goal is to have an object pool that uses multiple column prefabs instead of just one, right? The issue with your current code is that even though you declared a columns array, you're only filling it with instances of the single columnPrefab. Here's how to adjust things to support multiple prefab types:

Step 1: Update Your Public Variables

First, replace the single columnPrefab with an array of prefabs so you can assign multiple column types in the Inspector. We'll keep the columns array for storing the pooled objects:

public class ColumbPool : MonoBehaviour {
    public int columnPoolSize = 5;
    public GameObject[] columnPrefabs; // New array for multiple column types
    private GameObject[] columns; // Pooled objects storage
    private Vector2 objectPoolPosition = new Vector2(-15f,-25f);
    private float timeSinceLastSpawn;
    public float spawnRate = 4f;
    public float columnMin = -1f;
    public float columnMax = 3.5f;
    private float spawnXPosition = 10f;
    private int currentColumn = 0;

Step 2: Initialize the Pool with Multiple Prefabs

In the Start() method, instead of instantiating the same prefab every time, pick a random prefab from columnPrefabs each time you create a pooled object. This ensures your pool has a mix of column types:

void Start() {
    columns = new GameObject[columnPoolSize];
    for (int i = 0; i < columnPoolSize; i++) {
        // Pick a random prefab from your multi-prefab array
        int randomPrefabIndex = Random.Range(0, columnPrefabs.Length);
        GameObject selectedPrefab = columnPrefabs[randomPrefabIndex];
        // Instantiate the selected prefab into the pool
        columns[i] = Instantiate(selectedPrefab, objectPoolPosition, Quaternion.identity);
    }
}

Step 3: (Optional) Adjust Spawning Logic for More Variety

If you want even more randomness (instead of cycling through the pool in order), you can replace the currentColumn logic with a random index when spawning:

void Update() {
    timeSinceLastSpawn += Time.deltaTime;
    if (GameController.instance.gameOver == false && timeSinceLastSpawn >= spawnRate) {
        timeSinceLastSpawn = 0;
        float spawnYPosition = Random.Range(columnMin, columnMax);
        
        // Option 1: Keep cycling through the pool (now with mixed types)
        columns[currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);
        currentColumn++;
        if (currentColumn >= columnPoolSize) {
            currentColumn = 0;
        }

        // Option 2: Pick a random pooled object each time (more random variety)
        // int randomPoolIndex = Random.Range(0, columns.Length);
        // columns[randomPoolIndex].transform.position = new Vector2(spawnXPosition, spawnYPosition);
    }
}

Quick Tips:

  • Don't forget to assign multiple column prefabs to the columnPrefabs array in the Unity Inspector (leave it empty and you'll get errors!).
  • If you want a balanced pool (equal numbers of each prefab type), adjust the Start() loop to cycle through the columnPrefabs array instead of picking randomly:
    int prefabIndex = i % columnPrefabs.Length;
    GameObject selectedPrefab = columnPrefabs[prefabIndex];
    
  • Double-check that all your column prefabs have the necessary components (colliders, sprites, etc.) to work correctly in your game.

That should do it—your object pool will now contain multiple column types, and each spawn will pull a different prefab from the pool!

内容的提问来源于stack exchange,提问作者Emre001

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:26:00