请帮助我解决我的错误; NullReferenceException:对象引用未设置为对象的实例

时间:2018-07-26 10:36:30

标签: c# visual-studio unity3d

嗨,我已经努力了3天,试图解决我的代码中的错误; NullReferenceException:对象引用未设置为对象的实例 Parallaxerr.Shift()(在Assets / scripts / Parallaxerr.cs:134) Parallaxerr.Update()(位于Assets / scripts / Parallaxerr.cs:82)

这是我的代码

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


public class Parallaxerr : 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;//particle prewarm
    public Vector3 immediateSpawnPos;
    public Vector2 targetAspectRatio;

    float spawnTimer;
    float targetAspect;
    PoolObject[] poolObjects; //step 1 ; variable declared 
    GameManager game;

    void Awake()
    {

    }

    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++)
        {
            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()
    {
        Transform t = GetPoolObject();
        if (t == null) return;//if true , this indicates that poolSize is too small
        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;//if true , this indicates that poolSize is too small
        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++) //step 3 ; variable used
        {
            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;
    }
}

我知道声明了PoolObject,但是它不输出任何值或输出null。我试图通过使用以下方法为PoolObject分配一个值:PoolObject = GetComponent(); 但是我又得到了另一个错误:资产/脚本/Parallaxerr.cs(45,9):错误CS0118:Parallaxerr.PoolObject' is a type',但预期为“变量” 和Assets / scripts / Parallaxerr.cs(45,35):错误CS0131:分配的左侧必须是变量,属性或索引器

有人可以告诉我一种给PoolObject赋值的方法,以便我的代码可以正常工作

0 个答案:

没有答案