Unity(对象引用未设置为对象实例)错误* C#*

时间:2019-12-12 20:42:38

标签: c# unity3d

好的,所以我不确定确切发生了什么,我一直在寻找其他标题相同/相似的帖子,但是他们都不清楚我在做什么,我有一个公共GameObject到另一个对象,我试图从变量GameObjects组件(脚本)向数组添加变量,但是它一直给我这个错误,这是我的代码。

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

public class Dice : MonoBehaviour
{

    public int diceNumber = 0;
    public bool diceActive = true;
    public GameObject rollbar;

    // Start is called before the first frame update
    void Start()
    {
        onCreate();
    }

    // Update is called once per frame
    void Update()
    {

    }

    //Run this when the object is either created or activated (Basically when the dice becomes interactable)
    void onCreate()
    {
        //Insert the dice into the array
        rollbar.GetComponent<RollButton>().basicDice.Insert(diceNumber, gameObject);
    }
}

编辑: 检查器和滚动按钮脚本的屏幕截图 enter image description here

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

public class RollButton : MonoBehaviour
{

    int NumberOfDice = 0;
    public ArrayList basicDice;


    // Start is called before the first frame update
    void Start()
    {
        basicDice = new ArrayList();
    }

    // Update is called once per frame
    void Update()
    {

    }

    //Custom Function to roll the dice
    public void roll()
    {
        print(basicDice);
    }
}

0 个答案:

没有答案