• 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 Roncon93 · Apr 24, 2011 at 03:07 AM · nullreferenceexception

NullReferenceException: Object reference not set to an instance of an object

Hi guys, I'm wondering why am I getting this error at line 26? If I take off the "myText.enabled = false;" part then it works. But I want the text to disappear if the GameObject's Y rotation is between 270 and 0 or between 0 and 90.

var moveSpeed = 50; private var yRotation = 0; function Update () { var z = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed; transform.Translate(0, 0, z);

if (Input.GetAxis("Horizontal")) { yRotation += Input.GetAxis("Horizontal") * 2; transform.eulerAngles = Vector3(0, yRotation, 0);

 if (transform.eulerAngles.y > 90 && transform.eulerAngles.y < 270)
 {
   var myText : GUIText = gameObject.Find ("Wrong Way").GetComponent ("GUIText");
   myText.enabled = true;
 }
 if (transform.rotation.y < 90 || transform.rotation.y > 270)
 {
   myText.enabled = false;
 }

} }

Comment
Add comment · Show 1
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 Justin Warner · Apr 24, 2011 at 03:54 AM 0
Share

I formatted code for you, can you tell us the new line number? And I code-ifyed it. I don't get any error when I use it... (With my set up though).

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Justin Warner · Apr 24, 2011 at 03:56 AM

function Update () { if (Input.GetButtonUp("Fire1")) { var myText : GUIText = gameObject.Find ("Wrong Way").GetComponent ("GUIText");

   myText.enabled = true;
 }
 if (Input.GetButtonUp ("Fire2"))
 {
   myText.enabled = false;
 }

}

Doesn't work. But.

function Update () { var myText : GUIText = gameObject.Find ("Wrong Way").GetComponent ("GUIText"); if (Input.GetButtonUp("Fire1")) {

  myText.enabled = true;
 }
 if (Input.GetButtonUp ("Fire2"))
 {
   myText.enabled = false;
 }

}

Does work. Just put the var myText line outside of the if statement. =).

Comment
Add comment · 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
0

Answer by Joshua · Apr 24, 2011 at 03:57 AM

You declare the var myText in one if() and then reference it in the next. Put the var myText : GUIText..... line three lines higher and you should be good.

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 Roncon93 · Apr 24, 2011 at 01:44 PM 0
Share

Thanks, it worked. I thought that too and even moved the line, but it seems that I moved it a little bit too high. Thanks, again.

avatar image Joshua · Apr 24, 2011 at 02:20 PM 0
Share

Please select on of the answers as the correct one then, so the question won't get bumped up by the bot :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

NullReferenceException while trying to access script on object 2 Answers

instantiate prefab object to existing object 1 Answer

NullReferenceException in a GUI Dialog Method 1 Answer

Gameobject.find & my error NullReferenceException: Object reference not set to an instance of an object 1 Answer

NullReferenceException 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