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 CloverKitsune · Dec 03, 2012 at 03:22 AM · c#errortriggernullreferenceexceptionnull

Can't find source of trigger Null Reference Exception

I have a box (non-trigger, kinematic rigidbody) that passes through different triggers (also set to kinematic rigidbody). The programming seems to function fine, but I keep getting a Null Reference Exception error, even though I've checked several times to make sure all Inspector-set variables have been dragged and dropped in place, and the right tags and scripts are assigned to the objects. Two of my three trigger types work fine ("slicks" and "clouds" are good, "gates" give the error), but they are set up the same way and compiling returns no errors.

The problem line is in the box script: if (triggerEnter.GetComponent().isActive == true)

Commenting out the line also makes the errors disappear.

Tests to check that something is returned on triggering come back correct, so I don't understand why I'm getting this error. I'm still new to C#. Does anyone know what the problem could be? Here are the scripts:

BOX SCRIPT

  using UnityEngine;
     using System.Collections;
     
     public class Box_Script : MonoBehaviour {
         
             
     public bool normalTimeSpeed = true; // has the time scale been changed?
     public float timeFast = 1.5f;        // controls time scale alterations
     public float timeSlow = 0.75f;
                   
     //========================================
         
         void Update () 
         {    
 
         
     // GATE TRIGGERS
         
         void OnTriggerExit(Collider triggerObject)
         {        
             if(triggerObject.tag == "Gate" && triggerObject.GetComponent<GatesGeneral>().isActive == true)
             {    
                 print (triggerObject.GetComponent<GatesGeneral>().isActive);//TEST. PRINTS "TRUE" LIKE IT SHOULD
                 
                 triggerObject.GetComponent<GatesGeneral>().isActive = false;        // prevent repeated triggering
             }
         }
         
     // TIME CONTROL TRIGGERS
         
         void OnTriggerEnter(Collider triggerEnter)
                     
         {        
             if (triggerEnter.GetComponent<Speeders>().isActive == true)    // PROBLEM LINE
             {    
                 
                 if(triggerEnter.tag == "Slick")                                        // Slicks speed up time
                 
                 {        
                     triggerEnter.GetComponent<Speeders>().isActive = false;            // prevent repeated triggering
                     
                     if (normalTimeSpeed == true)                                    // time is normal
                     {
                         Time.timeScale = timeFast;                                    // speed up time
                         normalTimeSpeed = false;
                     }
                                                                 
                     else                                                            // time was already slow                                                            
                     {
                         Time.timeScale = 1;                                            // return to normal
                         normalTimeSpeed = true;
                         
                     }
                 }
                 else if(triggerEnter.tag == "Cloud")                                // Clouds slow down time
                 {
                     
                     triggerEnter.GetComponent<Speeders>().isActive = false;            // prevent repeated triggering
                     
                     if (normalTimeSpeed == true)                                    
                     {
                         Time.timeScale = timeSlow;
                         normalTimeSpeed = false;
                     }
                 
                     else                                                                         
                     {
                         Time.timeScale = 1;
                         normalTimeSpeed = true;
                     }
                 }
             }
         }
     }


GATE SCRIPT

 using UnityEngine;
 using System.Collections;
 
 public class GatesGeneral : MonoBehaviour {
     
     
 public bool isActive = true;    // Item is active. Set by Box script to block repeated triggering (such as for speed modifiers)    
 public Material gateNormalRef;    // normal gate material (this variable for Lerp)
 public Material gateYesRef;        // material for a gate that has been successfully passed thru
 private float timer = 0;        // for Lerp
     
     void Update()
     {
         if (tag == "Gate" && isActive == false)    // Item tagged Gate has been collided with
             {    
                 renderer.material.Lerp (gateNormalRef, gateYesRef, timer);    // turn green
                 timer += (3.5f * Time.deltaTime);
             }    
     }
 }


CLOUD/SLICK SCRIPT

 using UnityEngine;
 using System.Collections;
 
 public class Speeders : MonoBehaviour {
 
     
 public bool isActive = true;    // Item is active. Set by Box script to block repeated triggering (such as for speed modifiers)
 public Material slickRef;
 public Material cloudRef;    
 public Material nullRef;
 private float timer = 0;        // for Lerp
     
     
     void Update () 
     {
         
     // MATERIAL LERPS
         
             if (tag == "Slick" && isActive == false)
         {    
             renderer.material.Lerp (slickRef, nullRef, timer);            // fade out
             timer += (0.25f * Time.deltaTime);
         }
         
             if (tag == "Cloud" && isActive == false)
         {    
             renderer.material.Lerp (cloudRef, nullRef, timer);            // fade out
             timer += (0.75f * Time.deltaTime);
         }
     }
 }

   
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

1 Reply

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

Answer by coastwise · Dec 03, 2012 at 03:40 AM

I'm guessing you have some other object with a trigger collider that isn't a cloud (doesn't have a Speeders attached). This could be your track, finish line, anything really. This shouldn't be a major concern, and you can solve your null reference error by checking to see if the triggered object is the type you are looking for.

 if (triggerEnter.GetComponent<Speeders>() != null  && triggerEnter.GetComponent<Speeders>().isActive == true) {
     
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 CloverKitsune · Dec 03, 2012 at 04:07 AM 0
Share

That worked great and now I realize what I did. The Gate part of the Box script works fine because I checked the triggering object for the Gate tag, before using GetComponent on it. The Clouds and Slicks section checked for tags after GetComponent, so if I hit a Gate, it registers like any other trigger which means GetComponent is called on it, but the console complains due to the lack of the attached Speeder script.

Thanks!

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

10 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

Related Questions

how to CORRECTLY load all assets (using Resources.LoadAll) 1 Answer

NullReferenceException Error 6 Answers

NullReferenceException error in an array of objects 0 Answers

NullReferenceException: Object reference not set to an instance of an object DestroyByContact.OnCollisionEnter2D (UnityEngine.Collision2D coll) 1 Answer

C# - instance is not set to an instance of an object 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