0
votes

I have a class that I save a prefab in by manually adding it. I save it in the project view. The prefab is located in the Resources folder. I add this class to a gameobject using : "AddComponent". If the game object is already added to the stage, everything works great. However, if the game object is created recently, the prefabs in the class are not recognized, and I get an error : ArgumentException: The thing you want to instantiate is null.

proof image : http://postimg.org/image/mx5nnza4l/full/

Code :

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() {
    }
}

and the class where I save the prefabs looks like :

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

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

1 Answers

1
votes

The Import Settings of a script are only evaluated when you add the script to a GameObject manually in the editor.

They are not evaluated in any way when you add the script as a component programmatically (i.e. using GameObject.AddComponent()). Therefore, the member variables are null and you must set them programmatically, too.