Unable to access public variable from different script

I have a C# script trying to access a public float in another, but I am unable to make this work. The float I want to access is timeMultiplier in the GameController.cs script, which is attached to a GameController GameObject in my scene. I am trying to access that variable from the Enemy.cs script which is inherited by a script attached to a prefab. That prefab needs to use the variable.

using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour
{
	public float timeMultiplier;
void Update ()
{
	 timeMultiplier = Time.time / 4;
}
}

using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {

private float tMultiplier;
private GameController gameController;

void Start() {
	gameController = GameObject.Find("GameController").GetComponent<GameController>();
}

void Update(){
	tMultiplier = gameController.timeMultiplier;

}

I am getting an “Object reference not set to an instance of an object” error at the tMultiplier line.

Any ideas on what I’m missing? Thank you

You can use static variable :

using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour
{
    public static float timeMultiplier;
    void Update ()
    {
        timeMultiplier = Time.time / 4;
    }
}
 
 
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour
{
    private float tMultiplier;

    void Update()
    {
        tMultiplier = GameController.timeMultiplier;
    }
}

I would do it like this:

 using UnityEngine;
 using System.Collections;
 public class Enemy : MonoBehaviour {
 
 private float tMultiplier;
 public Gameobject gameControllerObject;
 
 void Start() {
     GameController gameController = gameControllerObject.GetComponent<GameController>() as GameController;

 }
 
 void Update(){
     tMultiplier = gameController.timeMultiplier;
 
 }

Then drag your gameController GameObject into the reference on your Enemy script. Hope that helps!