The question is answered, right answer was accepted
Reference clock from another script
Hey, I'm having a bit of trouble with passing a reference from one script to another. I have one that works at a clock/day night cycle, and I want to start work on a script that relies on the clock. Someone on reddit told me a method of calling the clock, but I'm getting error messages.
Basically, I have this written in the clock script (DayNightCycle):
public int GetCurrentTime(){
return currentTime;
}
And this is how I have the other script set up:
public class Crops : MonoBehaviour {
private float CycleLength;
private int cycleStage;
private DayNightCycle myClock;
public void Initialize(DayNightCycle clock){
myClock = clock;
}
// Use this for initialization
void Start () {
myClock.GetCurrentTime ();
cropCycleLength = 0;
cycleStage = 0;
}
// Update is called once per frame
void Update () {
if (myClock.GetCurrentTime() == 365) { // <----Error here
Debug.Log ("check");
}
}
}
This is the error I'm getting, which I understand to mean there's something being referenced that doesn't exist: NullReferenceException: Object reference not set to an instance of an object
I'm going to have a lot more scripts that rely on the clock, so could someone have a look and give me an idea of what I need to do to get this working?
Answer by UnityCoach · Apr 14, 2017 at 08:20 PM
Not sure, but it seems you never call Initialize(DayNightCycle clock)
.
Huh... seems simple enough... can you give me a quick example of how to do that?
Well, the question is, where is the clock you want to assign? You could add a "current" static property to your DayNightCycle class, that would return the current clock object. So, later any object that needs access to the current clock could simply tap into it. A bit like a Singleton pattern.
public class DayNightCycle : $$anonymous$$onoBehaviour // I assumed it is a $$anonymous$$onoBehaviour
{
static private DayNightCycle _current;
static private DayNightCycle current
{
get
{
if (_current == null) // if current isn't assigned
_current = FindObjectOfType<DayNightCycle>(); // we look for one
if (_current == null) // if none was found
{
GameObject dnc = new GameObject ("DayNightCycle"); // we create a game object
_current = doc.AddComponent<DayNightCycle>(); // and assign it the component
}
return _current;
}
set { _current = value; }
}
void Awake ()
{
current = this; // assigning self to current when one is in the scene.
}
}
So, in the other script, you could use the current property to initialise on Awake
void Awake ()
{
Initialize (DayNightCycle.current); //
}
Or, you could simply tap in the current property all the time.
void Start ()
{
DayNightCycle.current.GetCurrentTime ();
cropCycleLength = 0;
cycleStage = 0;
}
void Update ()
{
if (DayNightCycle.current.GetCurrentTime() == 365)
{
Debug.Log ("check");
}
}
Sorry... would you $$anonymous$$d breaking that down into novice speak? I'm still really new to scripting in Unity. Also, would it help to see my DayNightCycle script? I want to try and keep the coding as simple as I can for the time being.
Well, you need to access a DayNightCycle instance. You have a private reference to one in your Crops class
private DayNightCycle myClock;
But it is never assigned to. Although you assign it with your Initialize method, but that one is never called. You can call it from Awake (), but it expects a DayNightCycle for parameter, right? So you need to access a DayNightCycle instance somewhere. In my humble opinion, the best way to do this, is to use a Singleton, a unique instance of a class. You can also call it current or main if it's not so unique. But you do this with a static accessor. It allows you to call DayNightCycle.current, which returns the current instance if one is assigned, looks for one if none is assigned, and create one if none can be found.
If you copy/paste this code in your DayNightCycle class, it will give you a current accessor that you can use from anywhere, anytime.
public class DayNightCycle : $$anonymous$$onoBehaviour // I assumed it is a $$anonymous$$onoBehaviour
{
static private DayNightCycle _current;
static public DayNightCycle current // sorry I previously did a mistake, this is meant to be public
{
get
{
if (_current == null) // if current isn't assigned
_current = FindObjectOfType<DayNightCycle>(); // we look for one
if (_current == null) // if none was found
{
GameObject dnc = new GameObject ("DayNightCycle"); // we create a game object
_current = doc.AddComponent<DayNightCycle>(); // and assign it the component
}
return _current;
}
set { _current = value; }
}
void Awake ()
{
current = this; // assigning self to current when one is in the scene.
}
}
Follow this Question
Related Questions
c# coding question 1 Answer
Referencing and Executing Script Can't Be Done due to Not Being A Statement 0 Answers
I am trying to reference a bool from another script but the only value that it returns is false 0 Answers
How Do I Refer the origin point in C#? 1 Answer
Can't reference script. 1 Answer