Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Step 6 " Survival shooter" tutorial NullReference object help

Discussion in 'Scripting' started by Ninetitle, Jun 7, 2015.

  1. Ninetitle

    Ninetitle

    Joined:
    Jun 7, 2015
    Posts:
    12
    Hi,
    I'm learning unity and was trying this tutorial and I'm stuck at step 6.

    It seems that the PlayerHealth script can't find the health slider and the damage Image.

    Here is the code of PlayerHealth:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class PlayerHealth : MonoBehaviour
    6. {
    7.     public int startingHealth = 100;
    8.     public int currentHealth;
    9.     public Slider healthSlider;
    10.     public Image damageImage;
    11.     public AudioClip deathClip;
    12.     public float flashSpeed = 5f;
    13.     public Color flashColour = new Color(1f, 0f, 0f, 0.1f);
    14.  
    15.     Animator anim;
    16.     AudioSource playerAudio;
    17.     PlayerMovement playerMovement;
    18.     //PlayerShooting playerShooting;
    19.     bool isDead;
    20.     bool damaged;
    21.  
    22.  
    23.     void Awake ()
    24.     {
    25.         anim = GetComponent <Animator> ();
    26.         playerAudio = GetComponent <AudioSource> ();
    27.         playerMovement = GetComponent <PlayerMovement> ();
    28.         //playerShooting = GetComponentInChildren <PlayerShooting> ();
    29.         currentHealth = startingHealth;
    30.     }
    31.  
    32.  
    33.     void Update ()
    34.     {
    35.         if(damaged)
    36.         {
    37.             damageImage.color = flashColour;
    38.         }
    39.         else
    40.         {
    41.             damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
    42.         }
    43.         damaged = false;
    44.     }
    45.  
    46.  
    47.     public void TakeDamage (int amount)
    48.     {
    49.         damaged = true;
    50.  
    51.         currentHealth -= amount;
    52.  
    53.         healthSlider.value = currentHealth;
    54.  
    55.         playerAudio.Play ();
    56.  
    57.         if(currentHealth <= 0 && !isDead)
    58.         {
    59.             Death ();
    60.         }
    61.     }
    62.  
    63.  
    64.     void Death ()
    65.     {
    66.         isDead = true;
    67.  
    68.         //playerShooting.DisableEffects ();
    69.  
    70.         anim.SetTrigger ("Die");
    71.  
    72.         playerAudio.clip = deathClip;
    73.         playerAudio.Play ();
    74.  
    75.         playerMovement.enabled = false;
    76.         //playerShooting.enabled = false;
    77.     }
    78.  
    79.  
    80.     public void RestartLevel ()
    81.     {
    82.         Application.LoadLevel (Application.loadedLevel);
    83.     }
    84. }
    85.  
    I tried almost everything from checking the tags to changing the script with the completed one.
     
  2. chronochris

    chronochris

    Joined:
    Jun 7, 2015
    Posts:
    61
    There are a few things to Make sure. Make sure that PlayerHealth Script has the Health slider and Damage Image attached in the Inspector. Another thing to make sure, since I had this problem. Is that you are using the right scripts. There are Completed Scripts provided by unity. I would delete those. If you get several null reference exceptions after that. Well you were using the Unity Scripts.

    If these steps do not solve your Error, a more detailed error message, and images of the editor will be needed.
     
  3. Ninetitle

    Ninetitle

    Joined:
    Jun 7, 2015
    Posts:
    12
    Ok quick update:
    I already checked script and inspector and they are correct.
    My error are:

    NullReferenceException: Object reference not set to an instance of an object
    PlayerHealth.Update () (at Assets/Scripts/Player/PlayerHealth.cs:49)

    NullReferenceException: Object reference not set to an instance of an object
    PlayerHealth.TakeDamage (Int32 amount) (at Assets/Scripts/Player/PlayerHealth.cs:66)
    EnemyAttack.Attack () (at Assets/Scripts/Enemy/EnemyAttack.cs:67)
    EnemyAttack.Update () (at Assets/Scripts/Enemy/EnemyAttack.cs:51)

    GameObject (named 'healthSlider') references runtime script in scene file. Fixing!

    GameObject (named 'Fill') references runtime script in scene file. Fixing!

    GameObject (named 'damageImage') references runtime script in scene file. Fixing!

    and the scripts

    EnemyAttack
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyAttack : MonoBehaviour
    5. {
    6.     public float timeBetweenAttacks = 0.5f;
    7.     public int attackDamage = 10;
    8.  
    9.  
    10.     Animator anim;
    11.     GameObject player;
    12.     PlayerHealth playerHealth;
    13.     //EnemyHealth enemyHealth;
    14.     bool playerInRange;
    15.     float timer;
    16.  
    17.  
    18.     void Awake ()
    19.     {
    20.         player = GameObject.FindGameObjectWithTag ("Player");
    21.         playerHealth = player.GetComponent <PlayerHealth> ();
    22.         //enemyHealth = GetComponent<EnemyHealth>();
    23.         anim = GetComponent <Animator> ();
    24.     }
    25.  
    26.  
    27.     void OnTriggerEnter (Collider other)
    28.     {
    29.         if(other.gameObject == player)
    30.         {
    31.             playerInRange = true;
    32.         }
    33.     }
    34.  
    35.  
    36.     void OnTriggerExit (Collider other)
    37.     {
    38.         if(other.gameObject == player)
    39.         {
    40.             playerInRange = false;
    41.         }
    42.     }
    43.  
    44.  
    45.     void Update ()
    46.     {
    47.         timer += Time.deltaTime;
    48.  
    49.         if(timer >= timeBetweenAttacks && playerInRange/* && enemyHealth.currentHealth > 0*/)
    50.         {
    51.             Attack ();
    52.         }
    53.  
    54.         if(playerHealth.currentHealth <= 0)
    55.         {
    56.             anim.SetTrigger ("PlayerDead");
    57.         }
    58.     }
    59.  
    60.  
    61.     void Attack ()
    62.     {
    63.         timer = 0f;
    64.  
    65.         if(playerHealth.currentHealth > 0)
    66.         {
    67.             playerHealth.TakeDamage (attackDamage);
    68.         }
    69.     }
    70. }
    71.  
    PlayerHealth
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class PlayerHealth : MonoBehaviour
    6. {
    7.     public int startingHealth = 100;                            // The amount of health the player starts the game with.
    8.     public int currentHealth;                                   // The current health the player has.
    9.     public Slider healthSlider;                                 // Reference to the UI's health bar.
    10.     public Image damageImage;                                   // Reference to an image to flash on the screen on being hurt.
    11.     public AudioClip deathClip;                                 // The audio clip to play when the player dies.
    12.     public float flashSpeed = 5f;                               // The speed the damageImage will fade at.
    13.     public Color flashColour = new Color(1f, 0f, 0f, 0.1f);     // The colour the damageImage is set to, to flash.
    14.  
    15.  
    16.     Animator anim;                                              // Reference to the Animator component.
    17.     AudioSource playerAudio;                                    // Reference to the AudioSource component.
    18.     PlayerMovement playerMovement;                              // Reference to the player's movement.
    19.     //PlayerShooting playerShooting;                              // Reference to the PlayerShooting script.
    20.     bool isDead;                                                // Whether the player is dead.
    21.     bool damaged;                                               // True when the player gets damaged.
    22.  
    23.  
    24.     void Awake ()
    25.     {
    26.         // Setting up the references.
    27.         anim = GetComponent <Animator> ();
    28.         playerAudio = GetComponent <AudioSource> ();
    29.         playerMovement = GetComponent <PlayerMovement> ();
    30.         //playerShooting = GetComponentInChildren <PlayerShooting> ();
    31.      
    32.         // Set the initial health of the player.
    33.         currentHealth = startingHealth;
    34.     }
    35.  
    36.  
    37.     void Update ()
    38.     {
    39.         // If the player has just been damaged...
    40.         if(damaged)
    41.         {
    42.             // ... set the colour of the damageImage to the flash colour.
    43.             damageImage.color = flashColour;
    44.         }
    45.         // Otherwise...
    46.         else
    47.         {
    48.             // ... transition the colour back to clear.
    49.             damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
    50.         }
    51.      
    52.         // Reset the damaged flag.
    53.         damaged = false;
    54.     }
    55.  
    56.  
    57.     public void TakeDamage (int amount)
    58.     {
    59.         // Set the damaged flag so the screen will flash.
    60.         damaged = true;
    61.      
    62.         // Reduce the current health by the damage amount.
    63.         currentHealth -= amount;
    64.      
    65.         // Set the health bar's value to the current health.
    66.         healthSlider.value = currentHealth;
    67.      
    68.         // Play the hurt sound effect.
    69.         playerAudio.Play ();
    70.      
    71.         // If the player has lost all it's health and the death flag hasn't been set yet...
    72.         if(currentHealth <= 0 && !isDead)
    73.         {
    74.             // ... it should die.
    75.             Death ();
    76.         }
    77.     }
    78.  
    79.  
    80.     void Death ()
    81.     {
    82.         // Set the death flag so this function won't be called again.
    83.         isDead = true;
    84.      
    85.         // Turn off any remaining shooting effects.
    86.         //playerShooting.DisableEffects ();
    87.      
    88.         // Tell the animator that the player is dead.
    89.         anim.SetTrigger ("Die");
    90.      
    91.         // Set the audiosource to play the death clip and play it (this will stop the hurt sound from playing).
    92.         playerAudio.clip = deathClip;
    93.         playerAudio.Play ();
    94.      
    95.         // Turn off the movement and shooting scripts.
    96.         playerMovement.enabled = false;
    97.         //playerShooting.enabled = false;
    98.     }    
    99. }
    100.  
    Thanks for the help
     
  4. chronochris

    chronochris

    Joined:
    Jun 7, 2015
    Posts:
    61
    Everything looks right.
    Go to the Project Tab > Assets > _CompletedAssets, and delete the Scripts folder. I want to make sure that you aren't using a mix of scripts. Then I want you to take screenshots of the Player Object in the Heirarchy (with inspector), the Zombunny Prefab (with inspector), and DamageImage, and HealthSlider (again with inspector)
     
    Last edited: Jun 7, 2015
  5. Ninetitle

    Ninetitle

    Joined:
    Jun 7, 2015
    Posts:
    12
    ok here the images :
    1.png 2.png 3.png 4.png

    and strange thing I just noticed that the image for healthslide fill is not being saved ( No idea why).
     
  6. chronochris

    chronochris

    Joined:
    Jun 7, 2015
    Posts:
    61
    Okay, you have a missing/corrupt/something wrong with the UnityEngine.UI.dll. You are having problems with "UI" elements. And sure enough, something is wrong with the UnityEngine.UI.dll, You can see the error at the bottom of those screen caps, and causing your nullreference expceptions. I don't know if that triggers anything in your memory, that you may have done, but that's the issue. I would suggest uninstalling, and reinstalling Unity.

    Someone else might be better equipped to answer the correct path to take, as I don't know what could have caused this issue, or if anything stores in the Registry that wouldn't be good for uninstalling/reinstalling?
     
  7. Ninetitle

    Ninetitle

    Joined:
    Jun 7, 2015
    Posts:
    12
    Ok Searching in this tread I found the solution, Unity bug need to reimport all and that fixes it .

    P.S. Thanks to the community for the quick help.