Question by
SkullMaster259 · Jul 04, 2017 at 03:13 PM ·
textgetcomponentintintegertostring
NullReferenceException: Object Reference not set to an instance of an object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour {
public int Pistol = 0;
public int Assault_Rifle = 0;
public int Marine = 0;
public UnityEngine.UI.Text CPistol;
public UnityEngine.UI.Text CAssault_Rifle;
public UnityEngine.UI.Text CMarine;
void Start (){
CPistol = GetComponent<Text> ();
CAssault_Rifle = GetComponent<Text> ();
CMarine = GetComponent<Text> ();
}
void Update(){
CPistol.text = Pistol.ToString ();
CAssault_Rifle.text = Assault_Rifle.ToString ();
CMarine.text = Marine.ToString ();
}
}
How exactly am I supposed to fix this?
Comment
Answer by vir1234 · Jul 06, 2017 at 10:15 AM
Try This
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour
{
public int Pistol = 0;
public int Assault_Rifle = 0;
public int Marine = 0;
public Text CPistol;
public Text CAssault_Rifle;
public Text CMarine;
void Update()
{
CPistol.text = Pistol.ToString ();
CAssault_Rifle.text = Assault_Rifle.ToString ();
CMarine.text = Marine.ToString ();
}
}
assign CPistol,CassaultRifle, and Cmarine in the inspector
Your answer
Follow this Question
Related Questions
How do you change UI Text to an int? 4 Answers
Accessing a text variable from another script 0 Answers
Sync Var not syncing on client. 0 Answers
Stopwatch will not show time. 1 Answer