Unity3d Prefab保存在类中,不会仅出现在运行时创建的GO上

时间:2015-03-28 08:40:56

标签: unity3d

我有一个课程,我通过手动添加它来保存预制件。我将它保存在项目视图中。预制件位于Resources文件夹中。 我使用以下命令将此类添加到游戏对象:“AddComponent”。 如果游戏对象已经添加到舞台中,一切都很有效。但是,如果最近创建了游戏对象,则无法识别类中的预制件,并且出现错误: ArgumentException:您想要实例化的东西是null。

证明图片:http://postimg.org/image/mx5nnza4l/full/

代码:

using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {

    private static GameManager _instance;

    private bool _applicationIsQuitting = false;

    //=================== o0 ==============================
    // public MenuControl // The User Interface
    public CharacterData CHARDATA; // The cars data - contains all the data for all the different cars
    public MenuControl MENUS; // Contains the menus !
    public GameControl GAME_CONTROL; // The actual game - may refer to different levels / loading different levels

    //=================================================================================================
    // whenever it is added, runs the initialize function.
    void Awake() {
        DontDestroyOnLoad(gameObject);
        if(_instance == null) {
            Initialize();
        }
    }

    // Returns the game manager instance 
    public static GameManager Instance() {
        // returns an instance of the games manager
        if(_instance == null) {
             /*if (_applicationIsQuitting) {
                Debug.LogWarning("[Singleton] Instance '"+ typeof(GameManager) +
                                 "' already destroyed on application quit." +
                                 " Won't be created again - returning null.");
                return null;
            }*/

            //================================ 0o
            // if there is more than one Singleton of this - problem !
            if( FindObjectsOfType<GameManager>().Length > 1) {
                Debug.LogError("[Singleton] Something went really wrong " +
                               " - there should never be more than 1 singleton!" +
                               " Reopening the scene might fix it." +
                               " Not going to return null yet - but might change in the future.");
            }

            //================================ o0 
            // if there is no game manager
            _instance = GameObject.FindObjectOfType<GameManager>();
            if(_instance == null) {
                GameObject go = new GameObject("GameManagerGO");
                DontDestroyOnLoad(go);

                _instance = go.AddComponent<GameManager>() as GameManager;
            }
        }

        Debug.Log("returning instance ! :"+_instance);
        return _instance;
    }

    //==============================================================================================
    // Initializes the game content - UI and so on.
    private void Initialize() {
        if(CHARDATA == null) {
            CHARDATA = gameObject.AddComponent<CharacterData>() as CharacterData;
        }

        if(MENUS == null) {
            Debug.Log("Starting up the menus ! "+gameObject.name);
            // MENUS = gameObject.AddComponent<MenuControl>() as MenuControl;
            MENUS = gameObject.AddComponent<MenuControl>() as MenuControl;
        }

        if(GAME_CONTROL == null) {
            // GAME_CONTROL = gameObject.AddComponent<GameControl>() as GameControl;
        }
    }

    void OnDestroy() {
        _applicationIsQuitting = true;
    }

    //=============================
    // No constructor - simply using a protected version
    protected GameManager() {
    }
}

和我保存预制件的类看起来像:

void Start() {
    Invoke("DelayedMenuStart",1);
}

void DelayedMenuStart() {
    Instantiate(MenuCanvas);
    // create a new Canvas that holds all the menus 
    MenuCanvasObject = Instantiate(MenuCanvas);}

1 个答案:

答案 0 :(得分:0)

只有在编辑器中手动将脚本添加到GameObject时,才会评估脚本的Import Settings

当您以编程方式将脚本添加为组件时(即使用GameObject.AddComponent()),不会以任何方式评估它们。因此,成员变量为null,您也必须以编程方式设置它们。