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.