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 Sitrane · Oct 10, 2011 at 09:04 PM · null reference

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

I'm sure that this has been answered somewhere else but I haven't found it yet and I sort of need to turn in this project for class tonight.

I am using Will Goldstone's "Unity Game Development Essentials" for a class and I am having a weird error creep up. (See title)

Below is my code:

     private var doorIsOpen : boolean = false;
     private var doorTimer : float = 0.0;
     private var currentDoor : GameObject;
 
     var batteryCollect : AudioClip;
     var doorOpenTime : float = 3.0;
     var doorOpenSound : AudioClip;
     var doorShutSound : AudioClip;
     
 function Update () {
 
     var hit : RaycastHit;
     
         if(Physics.Raycast (transform.position, transform.forward, hit, 5)) {
         
         if(hit.collider.gameObject.tag=="outpostDoor" && doorIsOpen == false && BatteryCollect.charge >= 4){
             GameObject.Find("Battery GUI").GetComponent(GUITexture).enabled=false;
             currentDoor = hit.collider.gameObject;
             Door(doorOpenSound, true, "dooropen", currentDoor);
                 
             }
         }
         
         else if(hit.collider.gameObject.tag=="outpostDoor" && doorIsOpen == false && BatteryCollect.charge < 4){
             GameObject.Find("Battery GUI").GetComponent(GUITexture).enabled=true;
             TextHints.message = "The door seems to need more power...";
             TextHints.textOn = true;
         }
     
     if(doorIsOpen){
         doorTimer += Time.deltaTime;
         
         if(doorTimer > 3){
             Door(doorShutSound, false, "doorshut", currentDoor);
             doorTimer = 0.0;
         }
     }
 }
 /*
 function OnControllerColliderHit(hit : ControllerColliderHit) {
     if(hit.gameObject.tag == "outpostDoor" && doorIsOpen == false){
         currentDoor = hit.gameObject;
         Door(doorOpenSound, true, "dooropen", currentDoor);
     }
 }
 */
 
 function Door(aClip : AudioClip, openCheck : boolean, animName : String, thisDoor : GameObject){
     audio.PlayOneShot(aClip);
     doorIsOpen = openCheck;
     
     thisDoor.transform.parent.animation.Play(animName);
     
 }
 
 function OnTriggerEnter(collisionInfo : Collider){
 
     if(collisionInfo.gameObject.tag == "battery"){
         BatteryCollect.charge++;
         audio.PlayOneShot(batteryCollect);
         Destroy(collisionInfo.gameObject);
     }
 }
 
 @script RequireComponent(AudioSource)

For some reason I am getting this error:

NullReferenceException: Object reference not set to an instance of an object PlayerCollisions.Update () (at Assets/Scripts/PlayerCollisions.js:24)

But I am very confused because line 24, which is:

     else if(hit.collider.gameObject.tag=="outpostDoor" && doorIsOpen == false && BatteryCollect.charge < 4){

Will not work at all if "else" is there, but once I remove it, the error continues BUT it works like its suppose to.

I'm very confused, so if anyone would be able to look at this, I would greatly appreciate it.

Thanks!

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 Martijn Hendriks · Oct 10, 2011 at 09:30 PM 0
Share

I see batteryCollect and BatteryCollect, maybe this has something to do with it? On a side note: I always prefer to nest if-statements 'as much as' possible. $$anonymous$$eaning, putting the first two checks (which are the same in both cases) in one if-statement and then check for the charge property. You can then be sure that the first two are true when you check for the third.

avatar image Sitrane · Oct 10, 2011 at 09:38 PM 0
Share

I changed the batteryCollect and BatteryCollect and it didn't change anything.

As far as the code goes, I'm suppose to copy it straight from the book and as far as I can tell, this is what the book wants...I think

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DaveA · Oct 10, 2011 at 09:52 PM

I hate it when code isn't tested before it's published, unless it has such a disclaimer on it.

It should be 'batteryCollect' with a small b, because that's how the variable is declared above. That, or the hit doesn't have a collider, or the collider doesn't have a game object.

Why not run this in the debugger, where it will stop on this line, and you can inspect the variables and see what isn't existing? If you don't want to mess with the debugger, you can use Debug.Log to 'dump' the variables (before this problem line), to see what values. One of them will be null.

But it's probably that 'b' thing.

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 Sitrane · Oct 10, 2011 at 10:26 PM 0
Share

I did change everything to say 'batteryCollect' rather than the B.

$$anonymous$$y debugger doesn't come up for some reason... It's grayed out.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

NullReferenceException Error 1 Answer

C# Array Index Out of Range 1 Answer

How to import the object from server to unity 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 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