Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by smirlianos · Oct 09, 2013 at 09:56 AM · texttextmesh3dtext

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();
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image smirlianos · Oct 09, 2013 at 10:29 AM 0
Share
 #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);
 }
avatar image YoungDeveloper · Oct 09, 2013 at 11:37 AM 0
Share

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);
 }
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image smirlianos · Oct 09, 2013 at 10:49 AM 0
Share

What does this mean??

avatar image sed · Oct 09, 2013 at 10:52 AM 0
Share

Isn't it initialised to 0 by default?

avatar image meat5000 ♦ · Oct 09, 2013 at 10:53 AM 0
Share

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.

avatar image
0

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)

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Oct 09, 2013 at 10:58 AM 0
Share

@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 :)

avatar image sed · Oct 09, 2013 at 05:17 PM 0
Share

@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.

avatar image meat5000 ♦ · Oct 09, 2013 at 05:24 PM 0
Share

Indeed :) No harm done, I just published the one and cancelled the rest.

avatar image sed · Oct 09, 2013 at 06:39 PM 0
Share

So I stand informed now ;). Cheers.

avatar image sed · Oct 09, 2013 at 08:24 PM 0
Share

I hope mods don't have to publish each edit separetly, I had so many $$anonymous$$or formatting mistakes here.

Show more comments
avatar image
0

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Oct 09, 2013 at 08:03 PM 0
Share

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.

avatar image ArkaneX · Oct 09, 2013 at 08:19 PM 0
Share

@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...

avatar image meat5000 ♦ · Oct 09, 2013 at 08:23 PM 0
Share

Read this

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()?

avatar image ArkaneX · Oct 09, 2013 at 08:26 PM 0
Share

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.

avatar image meat5000 ♦ · Oct 09, 2013 at 08:29 PM 0
Share

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()?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Text map problem with 3d text 1 Answer

TextMesh Pro visible in editor but not in game window. 2 Answers

Cannot get 3D Text/Text Mesh to wrap or a attached collider used as a button to scale based on word count. 2 Answers

Insert 3d text to the front face of a cube GameObject 0 Answers

Engraving, Embossing, and Embellishing text on to models 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges