尽管所有内容都被正确引用,但Unity获得了空引用实例

时间:2019-06-01 18:20:45

标签: c# visual-studio unity3d

我有一个非常简单的代码,当我运行它时,它显示了一个空引用引用,我不知道为什么。你能帮忙吗?这是引起问题的类的代码。

我曾尝试将变量设置为public,然后将变量设置为统一检查器,但这也不起作用

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


public class BlockController : MonoBehaviour
{
public GameObject displayBlocksParent;
public GameObject userBlocksParent;
private float timer = 0;
public float timeToDisapear = 5;
private bool count = false;
// Start is called before the first frame update
void Start()
{
    count = true;
    userBlocksParent = GameObject.FindGameObjectWithTag("userParent");
    displayBlocksParent = 
 GameObject.FindGameObjectWithTag("displayParent");
 //the next line is CAUSING THE PROBLEM!!!
    userBlocksParent.SetActive(false);
}

// Update is called once per frame
void Update()
{
    if (count)
    {
        timer += Time.deltaTime;
        if (timer >= timeToDisapear)
        {
            displayBlocksParent.SetActive(false);
            userBlocksParent.SetActive(true);
            timer = 0;
            count = false;
        }
    }
}

public void LevelUp()
{
    displayBlocksParent.SetActive(true);
    userBlocksParent.SetActive(false);
    timer = 0;
count = true;
}

}

这是确切的错误提示信息:

NullReferenceException:对象引用未设置为对象的实例 BlockController.Start()(在Assets / Scripts / BlockController.cs:19处)

编辑:

标签userParent存在于游戏对象上: the image

0 个答案:

没有答案