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 ChickenReborn · Apr 10, 2012 at 08:35 PM · javascriptnullreferenceexception

Null Reference Exception confusion

Hello everybody!

I am presently having a lot of trouble with a script I have recently made. Here is the script below:

 #pragma strict
 
 private var goals : GameObject;
 
 function Update ()
 {
    if(goals.GetComponent(GoalScript1).goal == true)
    {
     if(goals.GetComponent(GoalScript2).goal == true)
     { 
          if(goals.GetComponent(GoalScript3).goal == true)
          {
         Debug.Log("Win!");
         Time.timeScale = 0;
          }
     }
 }

Basically, the script looks at a variable found in each of the scripts mentioned in the three nested if statements and if all of the three if statements come back true, then (for now) it Logs a Win and pauses the game. Later I will attach GUI stuff to it.

The problem is the line of the first if statement is coming up as a Null Reference Exception which, according to my research, means that it is looking for a Object or Component that does not exist. Of course, the item DOES exist.

I have three goal scripts each having the boolean variable goal and each are coming up true according to my Debug.Log in the individual scripts.

Please tell me what I'm doing wrong. I've checked my syntax a hundred times, I got the basic concept from the script here: http://www.unityscript.com/lessons1/lessons/Communicating_outside_of_your_script.html

I would appreciate any help you can give me!

Thanks a lot!

Dwayne

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 ChickenReborn · Apr 10, 2012 at 10:10 PM 0
Share

This is not an answer, it's just my final working script. It may change, however, if Gregzo or someone else can suggest a better function to put my if statements in. Thanks for everyone's help!! Y'all rock!!

 #pragma strict
 
 private var goal1 : GameObject;
 private var goal2 : GameObject;
 private var goal3 : GameObject;
 
 function Awake()
 {
     goal1 = GameObject.Find("GoalTrigger_1");
     goal2 = GameObject.Find("GoalTrigger_2");
     goal3 = GameObject.Find("GoalTrigger_3");
 }
 
 function Update ()
 {
     if(goal1.GetComponent(GoalScript1).goal == true)
     {
         if(goal2.GetComponent(GoalScript2).goal == true)
         { 
             if(goal3.GetComponent(GoalScript3).goal == true)
             {
                 Debug.Log("Win!");
                 Time.timeScale = 0;
             }
         }
     }
 }
avatar image Owen-Reynolds · Apr 11, 2012 at 12:42 AM 0
Share

You could read the scripts at the start (see Grezo's answer.) Consider it for new stuff you write -- can be easier to work with.

Also, do you really need three different goalScripts? For example, if they only have different point values, reuse the same script, but add public points : int; and select points for each.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by gregzo · Apr 10, 2012 at 09:02 PM

Hi!

First, I doubt you need to check that every frame. Try taking it out of the update function and checking only when you need to!

Second, do you have 3 sperate scripts, named GoalScript1, GoalScript2 and GoalScript3? Are they attached to your object? Can you see them attached in the inspector?

Last, try this very redundant version :

 var goalScript1 : GoalScript1;
 goalScript1 = goals.GetComponent(GoalScript1) as GoalScript1;
 Debug.Log(goalScript1.goal);

If you're still getting a null reference, then there is no script of type GoalScript1 attached to your goals object.

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 ChickenReborn · Apr 10, 2012 at 10:04 PM 0
Share

The thought behind putting it in the function Update was that at any time all of my goals might trigger and I would need to register the win ASAP. What other function should I put it in?

Thanks for your input though!

avatar image
-2

Answer by Lttldude · Apr 10, 2012 at 08:56 PM

Since you are using #pragma strict, the format should be

 goals.GetComponent.<GoalScript1>().goal 

Resources: http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html

Comment
Add comment · Show 5 · 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 gregzo · Apr 10, 2012 at 09:03 PM 0
Share

Not necessarily...

avatar image Eric5h5 · Apr 10, 2012 at 09:09 PM 0
Share

No. goals.GetComponent(GoalScript1).goal does the same thing as the generic version, but slightly faster. There's no reason to use the generic version of GetComponent when using Unityscript, #pragma strict or not. If there was any problem with the syntax, there wouldn't be a question about null reference exceptions, since the script wouldn't compile in the first place....

avatar image Lttldude · Apr 10, 2012 at 09:40 PM 0
Share

Thanks for the tip.

avatar image ChickenReborn · Apr 10, 2012 at 10:03 PM 0
Share

I did not know that! I heard that one ought to use #pragma strict if they're developing for mobile or iOS, or something like that, so I kept it.

avatar image Eric5h5 · Apr 10, 2012 at 10:12 PM 0
Share

@ChickenReborn: keeping #pragma strict is fine. Using GetComponent(Type) ins$$anonymous$$d of GetComponent.<Type>() is also fine, in fact it's preferable in Unityscript.

avatar image
0

Answer by rutter · Apr 10, 2012 at 08:59 PM

This is the line that's erroring out, yes?

 if(goals.GetComponent(GoalScript1).goal == true)

If so, we have two obvious possibilities:

  • goals is null

  • goals.GetComponent(GoalScript1) is null (implying there is no such component attached)

Looking at the script sample you've posted, it looks like you never set goals. Perhaps you could make that variable public and set it in the inspector? Or perform some quick lookup in Awake() using GameObject.Find(), more or less like this:

 function Awake()
 {
     goals = GameObject.Find("MyGoalsObject"); //fill in the right name
 }
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 gregzo · Apr 10, 2012 at 09:05 PM 0
Share

Indeed, if goals is private, you need to assign the object to it somehow...

avatar image ChickenReborn · Apr 10, 2012 at 09:59 PM 0
Share

What if I need to find three objects as I need to access three scripts, each of which is attached to its own Game Object?

Also, concerning "setting" the variable named goals, does that mean tell it what object to look for that the script is attached to, because I tried that. I created three separate variables (goal1, goal2 and goal3) and connected each to the game objects connected to the scripts I am trying to connect to and I still got errors.

Finally, I don't know why its giving me the Null Exception error on the one if statement and not on the other two, which are set up identically.

Thanks for the input!

avatar image ChickenReborn · Apr 10, 2012 at 10:02 PM 1
Share

Belay those questions! I created three separate variables, set them all in the Awake function and it WOR$$anonymous$$ED!! AWESO$$anonymous$$E THAN$$anonymous$$S!!

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

8 People are following this question.

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

Related Questions

Setting Scroll View Width GUILayout 1 Answer

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

Cannot access sprite of a gameobject 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Object reference not set to an instance of an object 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