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 boddole · Feb 09, 2014 at 03:06 PM · c#randomnullreferenceexceptionswitch

Trouble with Null Reference in Script

Hello everyone, I'm having a hard time tracking down a null reference error that is occuring between 2 scripts. Just so you know, sometimes the script will fail early (on the 2nd time it activates) or will fail late (after 10+ activations).

For your reference, here is the full error: NullReferenceException: Object reference not set to an instance of an object PushPlatform.Push () (at Assets/Scripts/Rooms/Room 12/PushPlatform.cs:20) PushPlatformManager+c__Iterator3.MoveNext () (at Assets/Scripts/Rooms/Room 12/PushPlatformManager.cs:52)

Here is the 1st script that the error references, the error occurs at: "if (hit.rigidbody.tag == "TDPlatform")"

 using UnityEngine;
 using System.Collections;
 
 public class PushPlatform : MonoBehaviour {
     public float pushPower; //100?, 
 
     void FixedUpdate () 
     {
         //Push();
     }
     
     public void Push ()
     {
         //if (Input.GetKey (KeyCode.P))
         //{
         //Debug.Log ("P");
         RaycastHit hit;
         Physics.Raycast(transform.position, -transform.up, out hit, 10f);
             Debug.DrawRay(transform.position,-transform.up, Color.red);
             if (hit.rigidbody.tag == "TDPlatform")
             {
                 hit.rigidbody.AddForceAtPosition (-transform.up * pushPower, hit.point, ForceMode.Impulse);
                 Debug.Log ("Pusher hit " + hit.rigidbody.name);
             }
 
             else
             {
                 Debug.Log ("the pusher hit something besides the platform");
             }
         //}
     }
 }
 

And here is the 2nd script it references, the error will change but it occurs on one of the case lines ( pushPlatformArray [0,1,2].Push();).

 using UnityEngine;
 using System.Collections;
 
 public class PushPlatformManager : MonoBehaviour {
     // this script goes on TDPlatform1stPlayerLayer, 
     public float waitBetweenFire; //xx, 
     public int randomNumber;
     public PushPlatform[] pushPlatformArray;
     public bool selectionInProgress;
 
 
     void Awake ()
     {
         selectionInProgress = false;
         pushPlatformArray = new PushPlatform[4];
         pushPlatformArray = gameObject.GetComponentsInChildren <PushPlatform>();
     }
 
 
 
 
     void FixedUpdate ()
     {
         Fire();
     }
 
 
     void Fire()
     {
         if (selectionInProgress == false)
         {
         StartCoroutine (TriggerPusher(waitBetweenFire));
         }
     }
 
 
     IEnumerator TriggerPusher (float time)
     {
         selectionInProgress = true;
         Debug.Log ("selectionInProgress is " + selectionInProgress);
         yield return new WaitForSeconds (time);
         randomNumber = Random.Range (0,3);
 
         switch (randomNumber)
         {
             case 0: 
             pushPlatformArray [0].Push();
             Debug.Log ("pushPlatformArray [0] should have pushed");
             break;
 
             case 1: 
             pushPlatformArray [1].Push();
             Debug.Log ("pushPlatformArray [1] should have pushed");
             break;
 
             case 2: 
             pushPlatformArray [2].Push();
             Debug.Log ("pushPlatformArray [2] should have pushed");
             break;
 
             case 3: 
             pushPlatformArray [3].Push();
             Debug.Log ("pushPlatformArray [3] should have pushed");
             break;
 
             default:
             Debug.Log ("ranndom number failed to trigger a push");
             break;
         }
         selectionInProgress = false;
         Debug.Log ("selectionInProgress is " + selectionInProgress);
     }
 }
Comment
Add comment
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

2 Replies

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

Answer by Linus · Feb 09, 2014 at 03:30 PM

 hit.rigidbody.tag

should be

 hit.transform.tag
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 boddole · Feb 09, 2014 at 03:37 PM 0
Share

$$anonymous$$aking this change seems to have fixed the problem, thank you very much. On a side note, since my script was wrong, why did it even work to begin with?

avatar image Linus · Feb 09, 2014 at 03:43 PM 0
Share

Unity did not get the problem before it tried to access hit.rigidbody.tag

avatar image boddole · Feb 09, 2014 at 03:55 PM 0
Share

Right, but then why would it work 2-10ish times before it failed - is what I'm really getting at.

avatar image Linus · Feb 09, 2014 at 04:28 PM 0
Share

Best guess would be that the raycasts did not hit anything the other times for whatever reason and that part of the code was not ran. hit would be null and just skipped to else not bothering to check if tag is part of rigidbody

avatar image boddole · Feb 10, 2014 at 08:57 AM 0
Share

I see, thanks for the clarification.

avatar image
1

Answer by Murkas · Feb 09, 2014 at 03:29 PM

The problem is, that the raycast is not hitting anything or at least nothing with a rigidbody. The raycast function is of type boolean, so you can check, if it hits anything. If it hit anything, you should check, if the hit object has a rigidbody, you can simply check this with if(hit.rigidbody). Try the following:

 if(Physics.Raycast(transform.position, -transform.up, out hit, 10f)){
   if(hit.rigidbody){
     if (hit.rigidbody.tag == "TDPlatform")
     {
       hit.rigidbody.AddForceAtPosition (-transform.up * pushPower, hit.point, ForceMode.Impulse);
       Debug.Log ("Pusher hit " + hit.rigidbody.name);
     }
     else
     {
       Debug.Log ("the pusher hit something besides the platform");
     }
   }else{
     //Here the hit object does not have a rigidbody
   }
 }else{
   //Here the raycast did not hit anything
 }
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 boddole · Feb 09, 2014 at 03:39 PM 0
Share

Although this was not the direct answer to my problem, I do appreciate it as it seems to just be good practice for the future (and will save me some future headaches), thank you.

avatar image Linus · Feb 09, 2014 at 03:46 PM 0
Share

It is good practice to check if the hitted object has the component you want to access.

I tend to do what you do though if I am only after checking for certain objects. Check if it is the object I want, and let unity crash horribly to let me know if I have miss placed a tag or forgotten to add the correct component.

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

20 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

Related Questions

NullReferenceException help 0 Answers

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

C# nullreferenceexception object reference not set 1 Answer

NullReference when accessing GameObject in array (C#) 1 Answer

C# Need Help Finding Where Null Reference Exception Is 2 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