Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Apr 17, 2017 at 09:07 PM by LadyWulff for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by LadyWulff · Apr 14, 2017 at 08:14 PM · scripting problemreferencing

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by UnityCoach · Apr 14, 2017 at 08:20 PM

Not sure, but it seems you never call Initialize(DayNightCycle clock).

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LadyWulff · Apr 14, 2017 at 11:26 PM 0
Share

Huh... seems simple enough... can you give me a quick example of how to do that?

avatar image UnityCoach · Apr 15, 2017 at 09:01 AM 0
Share

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");
     }
 }

avatar image LadyWulff UnityCoach · Apr 16, 2017 at 03:16 AM 0
Share

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.

avatar image UnityCoach LadyWulff · Apr 16, 2017 at 07:16 PM 0
Share

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.
      }
  }
Show more comments

Follow this Question

Answers Answers and Comments

126 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges