Quantcast
Channel: Latest Questions by jss367
Viewing all articles
Browse latest Browse all 12

Unable to access public variable from different script

$
0
0
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

Viewing all articles
Browse latest Browse all 12

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>