- Home /
int to string problem
Hello,
I want to change the text of a 3dText accordingly to the player's points. I have this script but it doesn't work. It gives me these errors:
NullReferenceException: Object reference not set to an instance of an object
System.Int32.ToString () (at /Applications/buildAgent/work/3df08680c6f85295/mcs/class/corlib/System/Int32.cs:672)
Test.Start () (at Assets/Test.js:9)
Script:
textandpoints.text = script1.scoreHold.ToString();
Answer by YoungDeveloper · Oct 09, 2013 at 10:14 AM
The code seems okay, where is this code located, inside Start() ? Show more code related to variables in your given line.
#pragma strict
function Start(){
var txtScore = GameObject.Find("TextScore");
if (txtScore)
{
var textandpoints : Text$$anonymous$$esh = txtScore.GetComponent(Text$$anonymous$$esh);
var script1 : ScoreHolder = txtScore.GetComponent("ScoreHolder");
textandpoints.text = script1.scoreHold.ToString();
}
}
ScoreHolder.Js
#pragma strict
var scoreHold : int;
function Awake () {
DontDestroyOnLoad (transform.gameObject);
}
You have not assigned scoreHold, you are basically trying add to string something that doesn't exist.
#pragma strict
var scoreHold : int = 0;
function Awake () {
DontDestroyOnLoad (transform.gameObject);
}
Answer by meat5000 · Oct 09, 2013 at 10:34 AM
Initialise scoreHold.
It means add a value to it at the start.
var scoreHold : int = 0;
This is giving it an 'Initial value' so we call it initialising.
Never make assumptions in program$$anonymous$$g. This leads to spurious results and unexpected behaviour.
Initialising is a way to make sure that that memory range represented by the variable has coherent information in it.
Answer by sed · Oct 09, 2013 at 10:57 AM
Are you sure the TextMesh was actually found? Or the other component you were looking for with .GetComponent(...)?
Those are the two obvious tracks to follow. In cases like this put a breakpoint in a line just before the first search operation.
function Start(){
// This can return null, put a breakoint in this line
var txtScore = GameObject.Find("TextScore");
if (txtScore)
{
var textandpoints : TextMesh = txtScore.GetComponent(TextMesh);//Here
var script1 : ScoreHolder = txtScore.GetComponent("ScoreHolder");//Here
textandpoints.text = script1.scoreHold.ToString();
}
}
It could be textandpoints.text giving you the null reference exception, or the script1.scoreHold because of either textandpoints or script1 being null.
Note: When using monodevelop always put a breakpoint in the line that "does something", it'll be a filled circle (as oposed to lighter fill color when breakpoint is put on, for example, a bracket line)
@sed. Your answer is not posted straight away as you need to wait in the moderation queue. Repeating the same answer doesn't get through any faster, it just fills up the mod queue and frustrates the mods :)
@meat5000 that's just fail design of this website then. I had no indicator that my answer got actually sent anywhere. $$anonymous$$ods would have less frustrating lifes if they asked a webdesigner to repair that bug.
Indeed :) No harm done, I just published the one and cancelled the rest.
I hope mods don't have to publish each edit separetly, I had so many $$anonymous$$or formatting mistakes here.
Answer by ArkaneX · Oct 09, 2013 at 08:00 PM
I have reproduced it, and I have to admit this is the most confusing NullReferenceException
I have ever seen...
The problem is related to the fact, that in this line:
var script1 : ScoreHolder = txtScore.GetComponent("ScoreHolder");
no component is returned, so script1
is null. You probably forgot to add ScoreHolder
script to your TextScore object. Btw - I suggest using txtScore.GetComponent(ScoreHolder)
(with type parameter instead of string).
And regarding my confusion - I don't understand the stack trace, suggesting that error occurred inside Int32.ToString()
method. In normal .NET application, exception would be pointing simply at line 9 of your script, because script1.scoreHold
part would cause exception.
EDIT: btw - in opposite to other answers/comments, there is no need to initialize scoreHold to 0. int
is value type, initialized to 0 by default.
I'm guessing the variable is created
var script1 : ScoreHolder
but initialisation fails
= txtScore.GetComponent("ScoreHolder");
But as the var script1 is declared as type ScoreHolder the variables exist but the values have not been transfered. Hence the error occurs on Int32.ToString() trying to convert nonsense int32 to a string on an existing but empty variable.
@meat5000 - I don't think so. If you do:
var script1 : ScoreHolder = null;
print(script1.scoreHold.ToString());
you get exception described by OP. But when you change second line to:
print(script1.scoreHold);
then you get NullReferenceException as well. And I would expect this to happen in first case as well...
You apparently can't use ToString() on an undefined or null variable.
Do you get the NullRef if you dont null ScoreHolder and leave out ToString()?
This article is related to JavaScript and describes toString() method, which is totally different from ToString() method in UnityScript.
EDIT: this whole problem is probably related to specific compilation of UnityScript project.
Fair play :) for shyts and giggles did you try the second suggestion?
Do you get the NullRef if you dont null ScoreHolder and leave out ToString()?