Search Unity

[NEED HELP FAST PLEASE] NullReferenceException

Discussion in 'Scripting' started by RealAspireGames, Jul 23, 2014.

  1. RealAspireGames

    RealAspireGames

    Joined:
    Dec 24, 2013
    Posts:
    265
    Hi.
    I am currently building a multiplayer game and using this asset. First off you guys did an amazing job with realistic fps prefab. Best thing you can buy for an FPS Game and MORE!
    I just have one problem.
    When i start my multiplayer game up and spawn into the game i keep getting an error everytime i try to pick up objects. Weapons, ammo, Health, etc

    The error is:
    NullReferenceException
    UnityEngine.GameObject.GetComponent[FPSPlayer] () (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineGameObject.cs:28)
    HealthPickup.PickUpItem () (at Assets/!Realistic FPS Prefab Files/Scripts/Items/HealthPickup.cs:26)
    UnityEngine.Component:SendMessageUpwards(String, SendMessageOptions)
    FPSPlayer:FixedUpdate() (at Assets/!Realistic FPS Prefab Files/Scripts/Player/FPSPlayer.cs:512)


    Here is the code for the 1 object i want to pick up, Any help would be great. Thank you!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HealthPickup : MonoBehaviour {
    5.     private GameObject playerObj;
    6.     private Transform myTransform;
    7.    
    8.     public float healthToAdd = 25.0f;
    9.     public bool removeOnUse = true;//Does this pickup disappear when used/activated by player?
    10.    
    11.     public AudioClip pickupSound;//sound to playe when picking up this item
    12.     public AudioClip fullSound;//sound to play when health is full
    13.    
    14.     public Texture2D healthPickupReticle;//the texture used for the pick up crosshair
    15.    
    16.     void Start () {
    17.  
    18.         myTransform = transform;//manually set transform for efficiency
    19.         //assign this item's playerObj value
    20.         playerObj = Camera.main.transform.GetComponent<CameraKick>().playerObj;
    21.     }
    22.    
    23.     void PickUpItem (){
    24.     FPSPlayer FPSPlayerComponent = playerObj.GetComponent<FPSPlayer>();
    25.    
    26.         if (FPSPlayerComponent.hitPoints < FPSPlayerComponent.maximumHitPoints){
    27.             //heal player
    28.             FPSPlayerComponent.HealPlayer(healthToAdd);
    29.            
    30.             if(pickupSound){AudioSource.PlayClipAtPoint(pickupSound, myTransform.position, 0.75f);}
    31.            
    32.             if(removeOnUse){
    33.                 //remove this pickup
    34.                 Object.Destroy(gameObject);
    35.             }
    36.            
    37.         }else{
    38.             //player is already at max health, just play beep sound effect
    39.             if(fullSound){AudioSource.PlayClipAtPoint(fullSound, myTransform.position, 0.75f);}      
    40.         }
    41.     }
    42. }
     
  2. TwoDeeSee

    TwoDeeSee

    Joined:
    Jul 22, 2014
    Posts:
    13
    Actually I think your problem is with FPSPlayer, can you post that code to?
     
  3. RealAspireGames

    RealAspireGames

    Joined:
    Dec 24, 2013
    Posts:
    265
    can anyone else help me with this problem?
     
  4. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    You should check if playerObj and FPSPlayerComponent are != null. I suspect one of the GetComponent<>'s is failing.
     
  5. RealAspireGames

    RealAspireGames

    Joined:
    Dec 24, 2013
    Posts:
    265
    How can i check if they are null, Should i just put that line of code in the void.start area?
     
  6. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    Code (csharp):
    1.  
    2.  
    3.  
    4. void PickUpItem (){
    5. //new
    6. if(playerObj){ // if player object exists
    7.     FPSPlayer FPSPlayerComponent = playerObj.GetComponent<FPSPlayer>();
    8.   }
    9.  
    10. //more new
    11. if(FPSPlayerComponent) // if there is a player component
    12. {
    13.        if (FPSPlayerComponent.hitPoints < FPSPlayerComponent.maximumHitPoints){
    14.            //heal player        
    15.            FPSPlayerComponent.HealPlayer(healthToAdd);                    
    16.            if(pickupSound){
    17.                   AudioSource.PlayClipAtPoint(pickupSound, myTransform.position, 0.75f);
    18.            }                      
    19.            if(removeOnUse){              
    20.                    //remove this pickup              
    21.                   Object.Destroy(gameObject);
    22.            }  
    23.        }else{
    24.            //player is already at max health, just play beep sound effect
    25.            if(fullSound){AudioSource.PlayClipAtPoint(fullSound, myTransform.position, 0.75f);}    
    26.        }
    27. }//end new if (no player component then don't try to do anything with one)
    28. } // end old function
    29.  
    30.