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();
}
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
↧