i get this error when i try playing my game NullReferenceException: Object reference not set to an instance of an object Click.Update () (at Assets/Scripts/Click.cs:13) here is my script it's c#\ using UnityEngine; using System.Collections; public class Click : MonoBehaviour { public UnityEngine.UI.Text fpc; public UnityEngine.UI.Text fatDisplay; public float fat = 0.00f; public int fatperclick = 1; void Update () { fatDisplay.text = "Fat: " + fat; fpc.text = "FPC: " + fatperclick; } public void Clicked(){ fat += fatperclick; } }
When you define public variables, they show up in the Unity Editor on the component in the Inspector. You need to drag and drop the appropriate objects into the component fields for the component to use them, or click the little circle next to them and choose from the list. You have two public Text fields, so your "Click" component will have two "Text" fields that start out empty.
When you add the Click component to an object, you will see this. See those two fields that say "none"? You need to drag into those fields the corresponding UI Text objects that you're trying to use.
Assets/Scripts/Click.cs(8,22): error CS0102: The type `Click' already contains a definition for `fat' now i got this error
here is my script using UnityEngine; using System.Collections; public class Click : MonoBehaviour { public UnityEngine.UI.Text fes; public UnityEngine.UI.Text fatDisplay; public float fat = 0.00f; public int fatperclick = 1; void Update () { fatDisplay.text = "Fat: " + fat; fes.text = "FES: " + fatperclick; } public void Clicked(){ fat += fatperclick; } }
You need to check that none of your fields say "None". They all need to have objects assigned. That's how to fix this.
using UnityEngine; using System.Collections; public class Click : MonoBehaviour { public UnityEngine.UI.Text fes; public UnityEngine.UI.Text fatDisplay; public float fat = 0.00f; public int fatperclick = 1; void Update () { fatDisplay.text = "Fat: " + fat; fes.text = "FES: " + fatperclick; } public void Clicked(){ fat += fatperclick; } }