简体   繁体   中英

Prefab null Unity C#

检查员查看预制件

The prefab is coming as null, even though I already assigned it in Unity. 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 ?

[UPDATE] 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?

Yes

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 ). 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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