C# 阵列仅在特定时间不可访问

C# 阵列仅在特定时间不可访问,c#,for-loop,unity3d,C#,For Loop,Unity3d,我目前正在Unity中构建一个视差背景效果,并从一些我正在尝试使用的起始代码开始。代码在大部分情况下都能正常工作,有些小问题我正在慢慢解决 我一直在测试公共和私人的各种统计数据,但似乎无法准确地找出我所面临的问题发生的原因和地点 当我运行同一个脚本并在每一帧调用Update()时,我希望具有相同大小的“poolObjects”长度。然而,当我以10的起始池大小调用它时,我得到10,然后2,然后0,然后10,2,0,等等 我截短它,但张贴什么,我认为是相关的在这里。希望你能帮我看看什么是显而易见的

我目前正在Unity中构建一个视差背景效果,并从一些我正在尝试使用的起始代码开始。代码在大部分情况下都能正常工作,有些小问题我正在慢慢解决

我一直在测试公共和私人的各种统计数据,但似乎无法准确地找出我所面临的问题发生的原因和地点

当我运行同一个脚本并在每一帧调用Update()时,我希望具有相同大小的“poolObjects”长度。然而,当我以10的起始池大小调用它时,我得到10,然后2,然后0,然后10,2,0,等等

我截短它,但张贴什么,我认为是相关的在这里。希望你能帮我看看什么是显而易见的

我确实看到了“在GetPool对象中”,但由于长度从不>0,因此我从未看到For循环中的内容,这是非常重要的。我不明白为什么poolObjects的长度会显示为0

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

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; }
    }
...
    public int poolSize;
    public float shiftSpeed;
    public float spawnRate;
...

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

    void Awake() {
        Configure();
    }

    void Start() {
        game = GameManager.Instance;
    }
...
    void Update() {
        Debug.Log(poolObjects.Length + "Len here of pool objects");
        if (game.GameOver) return;

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

    void Configure() {
        //spawning pool objects
        targetAspect = targetAspectRatio.x / targetAspectRatio.y;
        // targetAspect = Camera.main.aspect;
        // Debug.Log(targetAspectRatio.x +" " +  targetAspectRatio.y);
        poolObjects = new PoolObject[poolSize];
        for (int i = 0; i < poolObjects.Length; i++) {
            GameObject go = Instantiate(Prefab) as GameObject;
            Transform t = go.transform;
            t.SetParent(transform);
            t.position = Vector3.one * 1000;
            poolObjects[i] = new PoolObject(t);
        }

        if (spawnImmediate) {
            SpawnImmediate();
        }
    }

    void Spawn() {
        //moving pool objects into place
        Transform t = GetPoolObject();
        Debug.Log("In to Spawn" + t);
        if (t == null) return;
        Vector3 pos = Vector3.zero;
        pos.y = Random.Range(ySpawnRange.minY, ySpawnRange.maxY);
        pos.x = (defaultSpawnPos.x * Camera.main.aspect) / targetAspect;
        // Debug.Log("Spwaning");
        // Debug.Log(Camera.main.aspect);
        // Debug.Log(immediateSpawnPos.x + " " + immediateSpawnPos.y + " " + targetAspect);
        t.position = pos;
        // Debug.Log(pos);
    }

    void SpawnImmediate() {
        Transform t = GetPoolObject();
        if (t==null) return;
        Vector3 pos = Vector3.zero;
        pos.y = Random.Range(ySpawnRange.minY, ySpawnRange.maxY);
        pos.x = (immediateSpawnPos.x * Camera.main.aspect) / targetAspect;
        t.position = pos; 
        Spawn();
    }

    void Shift() {
        //loop through pool objects 
        //moving them
        //discarding them as they go off screen
        Debug.Log(poolObjects.Length + "Finding Length");
        for (int i = 0; i < poolObjects.Length; i++) {

            poolObjects[i].transform.position += -Vector3.right * shiftSpeed * Time.deltaTime;
            Debug.Log(poolObjects[i].transform.position);
            CheckDisposeObject(poolObjects[i]);
        }
    }

    void CheckDisposeObject(PoolObject poolObject) {
        //place objects off screen
        if (poolObject.transform.position.x < (-defaultSpawnPos.x * Camera.main.aspect) / targetAspect) {
            poolObject.Dispose();
            poolObject.transform.position = Vector3.one * 1000;
        }
    }

    Transform GetPoolObject() {
        //retrieving first available pool object
        Debug.Log("In GetPool Object" + poolObjects.Length);
        for (int i = 0; i < poolObjects.Length; i++) {
            Debug.Log("This is not showing up "+ poolObjects[i].inUse);
            if (!poolObjects[i].inUse) {
                poolObjects[i].Use();
                return poolObjects[i].transform;
            }
        }
        return null;
    }

}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类视差器:单行为{
类池对象{
公共转型;
公共图书馆;
公共池对象(转换t){Transform=t;}
public void Use(){inUse=true;}
public void Dispose(){inUse=false;}
}
...
公共投资规模;
公共浮动换档速度;
公共浮动利率;
...
浮点数计时器;
PoolObject[]池对象;
浮动目标光谱;
游戏经理游戏;
无效唤醒(){
Configure();
}
void Start(){
game=GameManager.Instance;
}
...
无效更新(){
Log(poolObjects.Length+“Len-here-of-pool对象”);
如果(game.GameOver)返回;
移位();
spawnTimer+=Time.deltaTime;
如果(繁殖计时器>繁殖速率){
繁殖();
产卵计时器=0;
}
}
void Configure(){
//产卵池对象
targetspect=targetspectratio.x/targetspectratio.y;
//targetspect=Camera.main.aspect;
//Debug.Log(targetspectratio.x+“”+targetspectratio.y);
poolObjects=新的PoolObject[poolSize];
for(int i=0;i
就其价值而言,池大小最初为:10。移位速度:1,繁殖率:20我猜场景中有3个视差器对象。通常对于合并的对象,你不会破坏它们,而是重新使用它们。但是,您肯定不想处置poolobject,但它所承载的对象?那么游泳池的场地将为下一个开放吗?@DanFein你能证实shingo所说的不是真的吗?哇,你们都是传奇人物!!谢谢你@Shingo,很高兴我这么问!!!不管它值多少钱,池大小最初是:10。移位速度:1,繁殖率:20我猜场景中有3个视差器对象。通常对于合并的对象,你不会破坏它们,而是重新使用它们。但是,您肯定不想处置poolobject,但它所承载的对象?那么游泳池的场地将为下一个开放吗?@DanFein你能证实shingo所说的不是真的吗?哇,你们都是传奇人物!!谢谢你@Shingo,很高兴我这么问!!!