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 unityquestions12345 · May 05, 2012 at 12:34 AM · errorreferencenullexceptionnull reference exception

Null Reference Exception Error

I am adding to the Car Tutorial a particle system that is using the following script. If you take note the script is actually modified from the Pickup script from the 3D Platform Game. I want it so that when the car collides with the particle system, the top speed is increased. This script is added to the particle system. When I run the program, I am getting an error:

NullReferenceException: Object reference not set to an instance of an object UpdateTopSpeed.OnTriggerEnter (UnityEngine.Collider col) (at Assets/Scripts/JavaScripts/UpdateTopSpeed.js:41)

Does anyone know how to fix this error or how to change the code around so that I don't have to worry about the error. Note I have not changed much from the tutorial besides adding the particle system and creating this script called UpdateSpeed.

Thank you!

 enum PickupType { Health = 0, FuelCell = 1 , TimeCell = 2}
 var pickupType = PickupType.TimeCell;
 var amount = 1;
 var sound : AudioClip;
 var soundVolume : float = 2.0;
 var carStatus : Car;
 
 
 private var used = false;
 private var mover : DroppableMover;
 
 function Start ()
 {
     // do we exist in the level or are we instantiated by an enemy dying?
     mover = GetComponent(DroppableMover);
 }
 
 function ApplyPickup (carStatus : Car)
 {
     // A switch...case statement may seem overkill for this, but it makes adding new pickup types trivial.
     switch (pickupType)
     {
         case PickupType.Health:
             carStatus.AddHealth(amount);
             break;
         
         case PickupType.FuelCell:
             carStatus.FoundItem(amount);
             break;
             
         case PickupType.TimeCell:
             carStatus.FoundItem(amount);
     }
     
     return true;
 }
 
 function OnTriggerEnter (col : Collider) {
     if(mover && mover.enabled) return;
     var carStatus : Car = col.GetComponent(Car);
     carStatus.GetComponent(Car).UpdateSpeed();
     
     //* Make sure we are running into a player
     //* prevent picking up the trigger twice, because destruction
     //  might be delayed until the animation has finished
     if (used || carStatus == null)
         return;
     
     if (!ApplyPickup (carStatus))
         return;
 
     used = true;
     
     // Play sound
     if (sound)
         AudioSource.PlayClipAtPoint(sound, transform.position, soundVolume);
         
     
     
     // If there is an animation attached.
     // Play it.
     if (animation && animation.clip)
     {
         animation.Play();
         Destroy(gameObject, animation.clip.length);
     }
     else
     {
         Destroy(gameObject);
     }
 }
 
 // Auto setup the pickup
 function Reset ()
 {
     if (collider == null)    
         gameObject.AddComponent(BoxCollider);
     collider.isTrigger = true;
 }
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 unityquestions12345 · May 05, 2012 at 12:43 AM 0
Share

I have established that the error is within these two lines:

var carStatus : Car = col.GetComponent(Car); carStatus.GetComponent(Car).UpdateSpeed();

avatar image maroonrs2 · May 07, 2012 at 12:30 AM 0
Share

Wait it's not a variable yet... You need to make it a variable b4 anything. Null means nothing. Referance means it cannot refer to something, error means failure to do... umm and the object must be a trigger/ have trigger flagged/ have trigger thing checked ont he collider.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · May 05, 2012 at 12:58 AM

If Car is a script and UpdateSpeed() a function of it, you could use this:

    ...
    var carStatus : Car = col.GetComponent(Car);
    carStatus.UpdateSpeed();
    ...
or just this:

    ...
    col.GetComponent(Car).UpdateSpeed();
    ...
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 unityquestions12345 · May 07, 2012 at 12:18 AM 0
Share

This didn't work. I am still getting the same error.

avatar image maroonrs2 · May 07, 2012 at 12:27 AM 0
Share

ALDO DO $$anonymous$$Y QUESTION ON THE UNANSWERED QUESTION PAGES! He doesnt have it as it... $$anonymous$$aybe you should try another script. Perhaps make your own?

avatar image aldonaletto · May 07, 2012 at 02:15 PM 0
Share

Probably GetComponent have not found the script Car in the object that entered the trigger, or the function UpdateSpeed doesn't exist/is misspelled. The object may not be the car, or the script Car isn't attached to it. You should use some print instructions to help finding the cause:

    ...
    if(mover && mover.enabled) return;
    var carStatus : Car = col.GetComponent(Car);
    if (carStatus){
        carStatus.UpdateSpeed();
    } else {
        print("Script Car not found in "+col.name);
    }
    ...
If the object detected doesn't have the script Car, this will print a warning and the name of this object.

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

6 People are following this question.

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

Related Questions

Despawner script returns null reference exception. 0 Answers

NullReferenceException Error 6 Answers

NullReferenceException - With Network PlayerSpawn 0 Answers

NullReferenceException: object not in inspector? 0 Answers

Object Pool object reference is null? 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