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 /
This question was closed Sep 18, 2014 at 02:44 PM by Graham-Dunnett for the following reason:

Duplicate Question - there are over 50 pages of questions about the same thing

avatar image
0
Question by david88888 · Sep 18, 2014 at 02:41 PM · javascriptnullreferenceexceptionontriggerenter

Object reference not set to an instance of an object, How can I fix it?

I follow the tutorial to make a game, and I done exactly with the tutorial. However some errors occur. When the player on trigger the coin, the coin can't destroy and the particle also not occur. How can I fix it. NullReferenceException: Object reference not set to an instance of an object ItemPickup+$OnTriggerEnter$17+$.MoveNext () (at Assets/Scripts/ItemPickup.js:34)

 enum PickupType                                                                
 {
         Grow      = 0,                                                              
     Key       = 1,                                                            
     Coin      = 2,                                                            
     Fireball  = 3,                                                            
     ExtraLife = 4,                                                             
     GameTime  = 5                                                             
 } 
 
 var pickupType               = PickupType.Grow;                                
 var pickupValue              : int              = 1;                         
 var itemParticle             : Transform;                                     
 var soundItemPickup          : AudioClip;                                      
 var soundDelay               : float            = 0.0;                          
 var soundRate                : float            = 0.0;                         
             
 private var playerGameObject : GameObject;                                       
 private var hudGameObject    : GameObject;                                       
 private var extraLifeEnabled : boolean          = false;                        
 
 function Start()                                                                 
 {
     playerGameObject = GameObject.FindWithTag("Player");                         
     hudGameObject    = GameObject.FindWithTag("hud");                            
 }
 function OnTriggerEnter(other : Collider)                                           
 {
     if ( other.tag == "collisionBoxBody" )                                         
     {
            var pProp = playerGameObject.GetComponent(PlayerProperties);
         
         ApplyPickup( pProp );
         
         renderer.enabled = false;
         
         if (itemParticle)
         {
             Instantiate(itemParticle, transform.position, transform.rotation);
         }
         if (soundItemPickup)
         {
             PlaySound (soundItemPickup, 0);
         }
         yield WaitForSeconds(audio.clip.length);
         if (extraLifeEnabled)
         {
             pProp.lives += pickupValue;
             extraLifeEnabled = false;
         }
         Destroy (other.gameObject);
     }
 }
 function ApplyPickup(playerStatus : PlayerProperties)                  
 {
     var hudConnect = hudGameObject.GetComponent(HudController);        
     
     switch (pickupType)                                                
     {
         case PickupType.Grow:                                          
             if (playerStatus.playerState != PlayerState.MarioFire)    
             {
                 playerStatus.playerState = PlayerState.MarioLarge;     
                 playerStatus.changeMario = true;                       
             }
             break;
         case PickupType.Key:                                           
             playerStatus.AddKeys (pickupValue);                        
             break;
         case PickupType.Coin:                                         
             playerStatus.AddCoin (pickupValue);                        
             hudConnect.coin += pickupValue;                            
             break;
         case PickupType.Fireball:                                      
             playerStatus.playerState =     PlayerState.MarioFire;         
             playerStatus.hasFire = true;                               
             playerStatus.changeMario = true;                           
             break;
         case PickupType.ExtraLife:                                     
             extraLifeEnabled = true;                                   
             break;
         case PickupType.GameTime:                                      
             // playerStatus.AddTime(pickupValue);                      
             break;
     }
 }
 function PlaySound(soundName : AudioClip, soundDelay : float)
 {
     if (!audio.isPlaying && Time.time > soundRate)
     {
         soundRate = Time.time + soundDelay;
         audio.clip = soundName;
         audio.Play();
     }
 }
Comment
Comments Locked · Show 1
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 gjf · Sep 18, 2014 at 09:01 AM 0
Share

did you check that playerGameObject is not null?

try adding some debug statements

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Null Reference Exception confusion 3 Answers

Split() into a fixed length array? 1 Answer

Why ExecuteInEditMode Always Causes NullReferenceException Errors Even In Clamping!! 2 Answers

function OnTriggerEnter with two conditionals help 1 Answer

Unity freezes when OnTriggerEnter occurs 3 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