0

I am trying to call a function from a script that is attached to a game object using an other script which is attached to a second game object.

The two game objects are called: Player, Game Manager.

Player has a script called Gold and Game Manager has a script called GameManager

Trying to call SetGold() function in Gold.cs from GameManager.cs, but I am getting the error: NullReferenceException: Object reference not set to an istance of an object

My current code: Gold.cs:

using UnityEngine;
using System.Collections;

public class Gold : MonoBehaviour {
    GameManager gm;

    public UnityEngine.UI.Text goldDisplay;

    private float _gold;

    void Start ()
    {
        gm = GameObject.Find("Game Manager").GetComponent<GameManager>();
        gm.Load();
    }

    void Update ()
    {
        UpdateGoldDisplay();
    }

    public void SetGold(float x)
    {
        _gold = x;
    }

    public float GetGold()
    {
        return _gold;
    }

    public void UpdateGoldDisplay()
    {
        SetGoldDisplay(GetGold().ToString());
    }
    public void SetGoldDisplay(string x)
    {
        goldDisplay.text = x;
    }
}

my other script (GameManager.cs)

using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {

    GameObject GO_Player;
    Gold gold;

    void Start ()
    {
        GO_Player = GameObject.Find ("Player");
        gold = GO_Player.GetComponent<Gold>();
    }

    void OnApplicationQuiot()
    {
        Save();
    }
    public void Load()
    {
        gold.SetGold(PlayerPrefs.GetFloat("gold", 50));
    }
    private void Save()
    {
        PlayerPrefs.SetFloat("gold", 1);
    }

    public void DeleteSaves()
    {
        PlayerPrefs.SetFloat("gold", 0);
    }
}

Thanks in Advance.

Henrik Varga
  • 55
  • 11
  • initialize the following from `GameManager gm;` to `gm = new GameManager()`; and do the same for the `gold` object or create a constructor – MethodMan Oct 23 '16 at 20:27
  • instead of new GameManager(); I had to use gm = gameObject.AddComponent(); For some reason when I hit Play it's generating the same error a lot of times in a second: NullReferenceException: Object reference not set to an instance of an object Gold.SetGoldDisplay (Single amount) (at Assets/Scripts/Player/Gold.cs:37) But I have drag&dropped the Text object to the gameObject in the hierarchy editor :\ – Henrik Varga Oct 23 '16 at 21:12
  • @MethodMan Cannot use the `new` kneyword on classes that inherits from `MonoBehaviour`. OPS current code looks fine. – Programmer Oct 24 '16 at 00:13
  • 1
    Can you post screenshot of your Hierarchy? Also, can you select your GameObject then take screenshot and upload it too? Just post the two links of your screenshot in the question – Programmer Oct 24 '16 at 00:15

1 Answers1

1

I will recommend to use initialization work in Awake(read more 1,2, 3). I am quoting one of the answer framgment here :

Usually Awake() is used to initialize if certain values or script are dependent of each other and would cause errors if one of them is initialized too late (awake runs before the game starts). Awake is also called only once for every script instance.

Your null reference exception make me think that you are trying to access it before initialization as you all initialization also written in Start.

Community
  • 1
  • 1
Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186