[SOLVED]C# Null Reference?

I have this error:
NullReferenceException: Object reference not set to an instance of an object
awk.Awake () (at Assets/Scipts/awk.cs:9)

Awk:

using UnityEngine;
using System.Collections;

public class awk : MonoBehaviour {

        void Awake()
    {
        //Setting the default values may change in future!
        GameManger.control.player_money = 0;
        GameManger.control.Player_damage = 1;
        GameManger.control.player_xp = 0;
        GameManger.control.xp_req = 5;
        GameManger.control.player_armor = 1;
        GameManger.control.player_health = 100;
        GameManger.control.player_potions = 0;
        GameManger.control.player_armorpen = 1;
        GameManger.control.player_level = 1;
        GameManger.control.player_xp_modifier = 1.5f;
        GameManger.control.boosterpacks = 0;
        GameManger.control.player_name = "Oachkatznal";
        GameManger.control.dead = false;
        GameManger.control.Quest = "HUEHUEHUEHUE";
        //Enemy
        GameManger.control.monster_name = "Unnamed";
        GameManger.control.monster_xp_reward = 1;
        GameManger.control.monster_damage = 2;
        GameManger.control.monster_armor = 1;
        GameManger.control.monster_armorpen = 1;
        GameManger.control.monster_health = 5;
        GameManger.control.monster_dead = false;
        GameManger.control.monster_gold_reward = 1;
        GameManger.control.xp_req = 5;
        GameManger.control.player_xp_modifier = 1.5f;
        GameManger.control.player_xp = 0;

       

    }
}

GameManger:

using UnityEngine;
using System.Collections;

public class GameManger : MonoBehaviour
{
   
        public static GameManger control;


        public string Quest;
        public double time;

        public int player_money;
        public int Player_damage;
        public int player_xp;
        public int xp_req;
        public int player_armor;
        public int player_health;
        public int player_potions;
        public int player_armorpen;
        public int player_level;
        public float player_xp_modifier;
        public int boosterpacks;
        public string player_name;
        public bool dead;

        //Enemy stats
        public string monster_name;
        public int monster_xp_reward;
        public int monster_gold_reward;
        public int monster_damage;
        public int monster_armor;
        public int monster_armorpen;
        public int monster_health;
        public bool monster_dead;


        void Awake ()
        {
        gameObject.tag = "Manger";
                if (control == null) {
                        DontDestroyOnLoad (gameObject);
                        control = this;
                } else if (control != this) {
                        Destroy (gameObject);
                }
        }

}

I have no idea why is this happening any help appriciated.

Move the awk script from Awake to Start. As a general rule, you shouldn’t have code that relies on other objects being already initialized in Awake.

See if that helps.

1 Like

If you need the awk script to initialize on Awake, you can change the script order execution. Or just do what @Zaladur said and move it to Start to allow GameManager to finish initializing OR make a static property that will initialize it when it gets called the first time, this is the Singleton Paradigm.

Thank you very much for the replies it solved the problem.