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 BZS · Jan 06, 2014 at 02:34 AM · javascriptvariableaccessing scripts

Help with my code?(NullReferenceException)[CLOSED]

I keep getting this error with my code.

NullReferenceException: Object reference not set to an instance of an object Gui.OnGUI () (at Assets/Essential Scripts/Gui.js:15)

It is on this line:

 if(script.selected1 == true)

Here is my code:

     var rType = 0;
     var script : Selecting_System;
     function OnGUI() {
 
         
 
         if (GUI.Button(Rect(10,70,50,30),"Click")){
             rType = 1;
             Debug.Log("Clicked the button with text");
     }
                 if(rType == 1)
                 {
                     if(script.selected1 == true)
                     {
                         Debug.Log("Success");
                     }
                 }else{
                 Debug.Log("Else");
                 }
 
 }

I'm trying to get it so that when I click the gui button the variable "rType" is equal to 1. Then I want it so when the variable from another script(selected1) is true and the variable "rType" is equal to 1 it will debug "success" It keeps giving me the error i typed above. If you could critique my script and/or explain why I am getting this error that would be great. Thanks in advanced.

Comment
Add comment · Show 2
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 sdgd · Jan 06, 2014 at 02:42 AM 0
Share

I suggest you look at this page it'll explain what NRE is

avatar image Invertex · Jan 06, 2014 at 03:36 AM 0
Share

Is the variable "selected1" set to public in your Selecting_System script? Can we see how your Selecting_System script looks?

Also, your if(rType == 1) statement should just be bundled with it's child if statement so you only have one "if", to simplify the code. Since the first statement isn't executing anything that the second statement would need, you're just checking if both are true before saying "Success".

   if((rType == 1) && (script.selected1))
   {
          Debug.Log("Success");
   }

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ArkaneX · Jan 06, 2014 at 11:06 AM

In the line you posted, NullReferenceException can be caused only by null script variable. So most probably you didn't set this variable in the inspector. Or, less likely, you set it to null somewhere else in your code.

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 BZS · Jan 06, 2014 at 01:37 PM 0
Share

Wow I feel really stupid now. Thanks!

avatar image
0

Answer by United_Unity · Jan 06, 2014 at 03:39 AM

First Check your spelling sounds stupid i know but that is my worst problem

second what are you doing with var script : selecting_system shouldn't that be

var script = selecting_system

and i believe you are missing a closing Bracket }

but i may very well be wrong with every thing is just said new to unity but know javascript

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

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

22 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 avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

How do I deselect an object when another is selected? 2 Answers

How do I deselect an object when another is selected? 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 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