简体   繁体   English

预制null Unity C#

[英]Prefab null Unity C#

检查员查看预制件

The prefab is coming as null, even though I already assigned it in Unity. 即使我已经在Unity中分配了预制,该预制仍为null。 I already searched in many places I still haven't found out where the error is exactly. 我已经在许多地方进行搜索,但仍然找不到错误的确切位置。

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

public class ButtonManager : MonoBehaviour 
{
    public GameObject myPrefab;
    public Transform Panel;

    public void distribution(List<InventoryItem> list){
        for (int i=0; i<list.Count; i++){

            Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity);
       }
    }
}

编辑器界面

Is it maybe the way I am assigning the button_manager_script ? 这可能是我分配button_manager_script吗?

[UPDATE] ButtonScriptOne [更新] ButtonScriptOne

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

public class ButtonScriptOne : MonoBehaviour {

    public ButtonManager button_manager_script;

    void Start () {
        button_manager_script = new ButtonManager();
    }

    public void test_function(){
        GlobalState.lazyLoad = true;
        var list_items = new List<InventoryItem>();
        list_items = DataStore.FindAllItems();

        Debug.Log("First: "+ list_items[0]);

        button_manager_script.distribution(list_items);
    }
}

Is it maybe the way I am assigning the button_manager_script? 这可能是我分配button_manager_script的方式吗?

Yes

1: button_manager_script = new ButtonManager(); 1: button_manager_script = new ButtonManager(); You can't do this. 你做不到 ButtonManager is a MonoBehaviour and Unity should have complained to you for calling new instead of AddComponent (or GetComponent ). ButtonManager是MonoBehaviour,Unity应该向您抱怨调用new而不是AddComponent (或GetComponent )。 I don't know why it didn't, or if you ignored that error and cleared it out of the console. 我不知道为什么没有,或者您是否忽略了该错误并从控制台中清除了该错误。

2: Because you somehow managed to do that anyway your non-attached component has a null myPrefab field because you never set it to anything from within ButtonScriptOne . 2:因为无论如何还是设法做到了这一点,因此未连接的组件具有空的myPrefab字段,因为您从未将其设置为ButtonScriptOne任何ButtonScriptOne

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM