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
0
Question by TheZenth1 · Jun 19, 2014 at 10:44 PM · javascriptnullreferenceexception

Why can't I access the TextMesh component of a GameObject?

Hi All,

I am currently attempting to create a Game Object with a TextMesh component when a user moves their mouse over another Game Object, and have this Text Mesh display information on the Game Object that was moused over. Once the mouse is not over the Game Object, the text mesh is then deleted.

The problem that I am encountering is that when I attempt to edit the text of the newly create TextMesh, it returns this error:

NullReferenceException: Object reference not set to an instance of an object StarGUI.OnMouseEnter () (at Assets/StarGUI.js:31) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32)

Edit! I've discovered that this only occurs with most of the Game Objects that this StarGUI script is attached to (the stars, which the text mesh is supposed to show information on). I am completely unsure as to why it occurs with most stars, but with a few it works fine..

And here is all of the code in the "StarGUI" file referenced:

 #pragma strict
 
 var guiBox : GameObject;
 var guiText1 : GameObject;
 
 var guiTextBase : GameObject;
 
 var systemData : GameObject;
 var    systemProperties : SystemProperties;
 
 var thisStar : GameObject;
 var thisStarSystem : StarSystem;
 
 function Start () 
 {
     Debug.Log("HEY");
     systemData = GameObject.Find("SystemData");
     systemProperties = systemData.GetComponent("SystemProperties");
     thisStar = gameObject;
 }
 
 function OnMouseEnter()
 {
     Debug.Log("OMGIMWORKING");
     guiBox = GameObject.CreatePrimitive(PrimitiveType.Cube);
     guiBox.name = "GUIBOX" + thisStar.name;
     guiBox.transform.position = Vector3(thisStar.transform.position.x + 3 * transform.localScale.x,thisStar.transform.position.y,transform.position.z);
     guiBox.transform.localScale = Vector3(thisStar.transform.localScale.x * 3,thisStar.transform.localScale.y * 4,thisStar.transform.localScale.z);
     guiText1 = Instantiate(guiTextBase);
     guiText1.name = "GUITEXTTHING";
     guiText1.GetComponent(TextMesh).text = thisStarSystem.name + "\nPart of " + thisStarSystem.group.name + "\n" + thisStarSystem.numPlanets + " Planets";
     guiText1.transform.position = guiBox.transform.position;
     guiText1.transform.localScale = Vector3(guiBox.transform.localScale.x/10,guiBox.transform.localScale.y/15,0.1);
 }
 
 function OnMouseExit()
 {
     Destroy(guiText1);
     Destroy(guiBox);
 }    
 
 function Update () 
 {
 
 }


Does anyone know what the problem is, or at least the right direction to go in to solve it?

Thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jeff-Kesselman · Jun 19, 2014 at 10:49 PM

Line 31 has a LOT of references. You need to determine what the problem really is. First break it up like this:

 function OnMouseEnter()
 {
     Debug.Log("OMGIMWORKING");
     guiBox = GameObject.CreatePrimitive(PrimitiveType.Cube);
     guiBox.name = "GUIBOX" + thisStar.name;
     guiBox.transform.position = Vector3(thisStar.transform.position.x + 3 * transform.localScale.x,thisStar.transform.position.y,transform.position.z);
     guiBox.transform.localScale = Vector3(thisStar.transform.localScale.x * 3,thisStar.transform.localScale.y * 4,thisStar.transform.localScale.z);
     guiText1 = Instantiate(guiTextBase);
     guiText1.name = "GUITEXTTHING";
     guiText1.GetComponent(TextMesh).text = 
           thisStarSystem.name + "\nPart of " + 
           thisStarSystem.group.name + "\n" + 
           thisStarSystem.numPlanets + " Planets";
     guiText1.transform.position = guiBox.transform.position;
     guiText1.transform.localScale = Vector3(guiBox.transform.localScale.x/10,guiBox.transform.localScale.y/15,0.1);
 }

Then run and see what line the error is on. Thsi will tell you whether the problem is really thisStarSystem,thisStarSystem.group or guiText1.GetComponent(TextMesh)

If its still on line 31 then insert a Debug.Log() just before it to print the value of guiText1 to make sure that thsi isnt what is null.

Finally, print guiText1.GetComponent(TextMesh) and see if it is returning null.

Once you actually know what is null, then we can help you figure out why...

Comment
Add comment · Show 1 · 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 TheZenth1 · Jun 19, 2014 at 11:55 PM 0
Share

Thank you; I've followed your advice, and I have found that all the other references, including guiText1, do not return null; but interestingly enough, neither does calling the Text$$anonymous$$esh by Debug.Log(guiText1.GetComponent(Text$$anonymous$$esh)). What I see from this Debug.Log is this: GUITEXTTHING (UnityEngine.Text$$anonymous$$esh) UnityEngine.Debug:Log(Object) StarGUI:On$$anonymous$$ouseEnter() (at Assets/StarGUI.js:32) UnityEngine.Send$$anonymous$$ouseEvents:DoSend$$anonymous$$ouseEvents(Int32, Int32) Stranger still, I still get this error: NullReferenceException: Object reference not set to an instance of an object StarGUI.On$$anonymous$$ouseEnter () (at Assets/StarGUI.js:34) UnityEngine.Send$$anonymous$$ouseEvents:DoSend$$anonymous$$ouseEvents(Int32, Int32)

And yet I do not get it when I call Debug.Log(guiText1.GetComponent(Text$$anonymous$$esh).text);

I still am baffled as to what the problem is. Do you know what it could be?

Thanks

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to pass function as parameter to function? 1 Answer

How to delete new'd arrays? 2 Answers

Null Reference Exception confusion 3 Answers

Split() into a fixed length array? 1 Answer

Why ExecuteInEditMode Always Causes NullReferenceException Errors Even In Clamping!! 2 Answers


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