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 Domain · Feb 11, 2014 at 04:38 AM · script error

Javascript - NullReferenceException: Object reference not set to an instance of an object levelclick.Start () (at Assets/Scripts/levelclick.js:29)

I'm making a level menu. I have a variable in PlayerPrefs called 'cleared', and it reads that, and for each level button, it checks cleared to see if it's high enough for that, and enables/disables the button accordingly. I don't have it for the level 1 button, because it should be enabled no matter what. HOWEVER! I keep getting this error, and the game works fine anyways, but it's annoying. When I click on the error in the console, it highlights the level 1 button object... which makes no sense because the script doesn't say anything about that gameObject at all. As you can see from the title it says the error's on line 29 (which is weird because level 29 is like all the rest of the lines). The game works fine anyways but it's annoying as hell. Can anyone spot the problem? Thanks!

 #pragma strict
 
 var loading : GameObject;
 var level = "";
 private var loadinglevel = false;
 private var levelsavailable = 0;
 private var level2button : GameObject;
 private var level3button : GameObject;
 private var level4button : GameObject;
 private var level5button : GameObject;
 private var level6button : GameObject;
 private var level7button : GameObject;
 
 function Start () {
     loading = GameObject.Find("loadingscreen");
     loading.renderer.material.color.a = 0;
     level2button = GameObject.Find("level2");
     level3button = GameObject.Find("level3");
     level4button = GameObject.Find("level4");
     level5button = GameObject.Find("level5");
     level6button = GameObject.Find("level6");
     level7button = GameObject.Find("level7");
     levelsavailable = PlayerPrefs.GetInt("cleared") + 1;
     
     if (levelsavailable >= 2) {
         level2button.SetActive(true);
     }
     else {
         level2button.SetActive(false);
     }
     
     if (levelsavailable >= 3) {
         level3button.SetActive(true); 
     }
     else {
         level3button.SetActive(false); 
     }
 }
 
 
 function OnMouseDown () {
     gameObject.renderer.material.color = Color(0.666, 0.666, 0.666);
 }
 
 function OnMouseUp () {
     gameObject.renderer.material.color = Color(1, 1, 1);
     loadinglevel = true;
 }
 
 function FixedUpdate () {
     if (loadinglevel) {
         loading.transform.position.z = -5;
         if (loading.renderer.material.color.a >= 1) {
             loadinglevel = false;
             Application.LoadLevel(level);
         }
         else {
             loading.renderer.material.color.a += .02;
         }
     }
 }
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 GameVortex · Feb 11, 2014 at 08:46 AM 0
Share

The only object at line 29 that can be null is the level2Button, so debug it and find out why it does not have a value.

This might help: http://answers.unity3d.com/questions/528288/null-reference-exception-what-is-it-and-why-do-i-g.html

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by sumeetkhobare · Feb 11, 2014 at 10:28 AM

The error is thrown because, while executing the Start() function the GameObject is not created yet (or is not fully activated). That's why , the GameObject.Find() has returned a null value to 'level2button'. and you are trying to deactivate that object which is null.

I suggest you run the if-else code snippet in Update(). I know you want to execute it only once, so use some flag.

Mark as solved if this helps..

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 Domain · Feb 12, 2014 at 04:02 AM 0
Share

I've worked some more on the code and now I've got 27 level buttons, but level2button is still the only one that gives me problems.

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

Endless rendering doesnt work 0 Answers

Why can't I edit C# script although I have monodevelop installed? PLEASE HELP 1 Answer

Update cannot be coroutine? 2 Answers

C# script works wrong ( GUI ) How to fix it ? 1 Answer

Please. Help me out :( 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