Reputation: 65
So I upgraded to Unity 5.3.2 last night, and now I can't drag a UI Text object into a script in the inspector. (Images below)
There are no compiler errors, except at runtime. I get this error every time:
NullReferenceException: Object reference not set to an instance of an object
Scoring.updateScores () (at Assets/Scripts/Scoring.js:21)
I have the javascript code below. The comments are things I have tried. Please ask for and needed clarification.
#pragma strict
var score : int;
//var scoreScore : GameObject;
//var highScore : GameObject;
var scoreScore : UnityEngine.UI.Text;
var highScore : UnityEngine.UI.Text;
function Start () {
//scoreScore = GameObject.Find("scoreScore").GetComponent.<UnityEngine.UI.Text>();
//highScore = GameObject.Find("highScore").GetComponent.<UnityEngine.UI.Text>();
score = 0;
updateScores();
}
function updateScores() {
if (score >= PlayerPrefs.GetInt("highScore")) {
PlayerPrefs.SetInt("highScore", score);
}
scoreScore.text = "" + score.ToString();
highScore.text = "" + PlayerPrefs.GetInt("highScore");
}
Upvotes: 1
Views: 1989
Reputation: 363
Drag and drop those text objects to your project, make them prefab. Then you can assign them.
Upvotes: 0
Reputation: 701
When you look at a script in the Inspector (by clicking on it within the Assets folder), you can set the default objects for that script. These objects can only be from your Sssets- it will not allow you to drag objects from within a certain scene. You can only drag objects from a scene to instances of scripts within that scene (e.g. a Scoring script attatched to a certain GameObject).
Try creating a GameObject, attaching the Scoring script to it, selecting that GameObject in the inspector, and the dragging the UIText elements to the instance of the script you attached to that GameObject.
Upvotes: 1