Singleton Problem
I'm new to coding in unity and wanted to transfer data from one script to another. I followed the tutorial i learned this from step by step(to the best of my knowledge) and I don't get any compiler errors but when I try to transfer the data i get this error:
NullReferenceException: Object reference not set to an instance of an object Button.jar () (at Assets/Scripts/Button.cs:8) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:144) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:621) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:756) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269) UnityEngine.EventSystems.EventSystem:Update()
Here are my Scripts
This i the script i want the data to be stored on:
using UnityEngine;
using System.Collections;
public class BodySpawner : MonoBehaviour {
public static BodySpawner control;
public int money;
public int rate;
}
This is the script I want to modify the data:
using UnityEngine;
using System.Collections;
public class Button : MonoBehaviour {
public void jar ()
{
Debug.Log(BodySpawner.control.money);
}
}
@$$anonymous$$enmidgets
using UnityEngine;
using System.Collections;
public class BodySpawner : $$anonymous$$onoBehaviour {
private static BodySpawner control;
public int money;
public int rate;
void Start()
{
money = 0;
rate = 0;
}
public static BodySpawner SharedInstance()
{
return control;
}
}
using UnityEngine;
using System.Collections;
public class $$anonymous$$yButton : $$anonymous$$onoBehaviour {
public void jar ()
{
Debug.Log(BodySpawner.SharedInstance().money);
}
}
@saud_ahmed020 im sorry but that didnt work, i got the same error.
@$$anonymous$$enmidgets You are not allowed to make a class with defined keywords unless you have different namespace. So, just change the name of class "Button" to something other like "$$anonymous$$yButtons".