Object reference not set to an instance of an object and one other error.

I have two strange errors, both of which worked fine earlier, but now don’t for reasons I don’t understand.
The most basic one is that basically everything I write returns with the error above, saying it’s not set to an instance of an object and I don’t get why. Both the time(Clock) I created and the startmenu give these errors. The other strange issue I have is that when I did Debug.Log(currentTime); to troubleshoot, I saw that it works fine, but every second time I do it, it returns null instead of returning currentTime.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Main : MonoBehaviour {

GameObject systemClock;
Text currentTime;
GameObject startMenu;
Image startMenuUI;
CanvasRenderer startMenuRenderer;
bool menuState;

// Use this for initialization
void Start () {

startMenu = GameObject.Find(“StartMenu”);
systemClock = GameObject.Find(“Time”);
currentTime = systemClock.GetComponent();
startMenuUI = startMenu.GetComponentInChildren();
startMenu.SetActive(false);

}

// Update is called once per frame
void Update () {

}

private void FixedUpdate()
{
Debug.Log(currentTime);
//currentTime.text = System.DateTime.Now.ToString(“HH:mm”);
//Debug.Log(System.DateTime.Now.ToString(“HH:mm”));
}

public void startMenuClicked()
{
if(menuState == false)
{
startMenu.SetActive(true);
menuState = true;
}
else if (menuState == true)
{
startMenu.SetActive(false);
menuState = false;
}

}

public void clickedBackground()
{
if(startMenuUI.enabled == true)
{
startMenuUI.enabled = false;
}
}
}

You could format you code in the question
For example:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Main : MonoBehaviour {

GameObject systemClock;
Text currentTime;
GameObject startMenu;
Image startMenuUI;
CanvasRenderer startMenuRenderer;
bool menuState;

// Use this for initialization
void Start () {

startMenu = GameObject.Find("StartMenu");
systemClock = GameObject.Find("Time");
currentTime = systemClock.GetComponent<Text>();
startMenuUI = startMenu.GetComponentInChildren<Image>();
startMenu.SetActive(false);


}

// Update is called once per frame
void Update () {

}

private void FixedUpdate()
{
Debug.Log(currentTime);
//currentTime.text = System.DateTime.Now.ToString("HH:mm");
//Debug.Log(System.DateTime.Now.ToString("HH:mm"));
}

public void startMenuClicked()
{
if(menuState == false)
{
startMenu.SetActive(true);
menuState = true;
}
else if (menuState == true)
{
startMenu.SetActive(false);
menuState = false;
}

}

public void clickedBackground()
{
if(startMenuUI.enabled == true)
{
startMenuUI.enabled = false;
}
}
}

It will be much easier for us to read it.

The solution for both of your exceptions. You didn’t name objects with the names you are looking for:

In this two lines you are trying to find two gameobjects with a specific names:

startMenu = GameObject.Find("StartMenu");
systemClock = GameObject.Find("Time");

Find will find for a gameobject by it’s name and will return the name.
For example you are searching for “Time”
If you don’t have in the Hierarchy any GameObject with the name Time you will get null.

So in this line:

currentTime = systemClock.GetComponent<Text>();

systemClock is null.

You can see here:

I clicked in the Menu on GameObject then Create Empty
So it’s adding GameObject
Now i click on the GameObject and change the name to Time
Now your script will fine it.

Same idea with the StartMenu
Create a GameObject in the Hierarchy and rename it to StartMenu