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
  4. Dismiss Notice

PowerUps not working OnTriggerEnter?

Discussion in 'Scripting' started by ltrout1999, Feb 10, 2016.

  1. ltrout1999

    ltrout1999

    Joined:
    Oct 11, 2015
    Posts:
    125
    So I decided to create a list so I could use Random.Range to pick a random power up out of the ones that are activable, and each time I go into the collider, it says this:
    Code (csharp):
    1.  
    2. NullReferenceException: Object reference not set to an instance of an object
    3. PlayerInfo.PlayerMovement.OnTriggerEnter (UnityEngine.Collider collider) (at Assets/Scripts/PlayerTank/PlayerMovement.cs:111)
    4.  
    Here are my classes that are handling the Power Ups:
    PlayerMovement.cs
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using EnemyInfo;
    5. using Shell;
    6. using UnityEngine.UI;
    7. using System.Collections.Generic;
    8.  
    9. namespace PlayerInfo
    10. {
    11.     public class PlayerMovement : MonoBehaviour
    12.     {
    13.         public float Speed = 10f;
    14.         public float TurnSpeed = 180f;
    15.         public AudioSource MovementAudio;
    16.         public AudioClip EngineIdling;
    17.         public AudioClip EngineDriving;
    18.         public float PowerUpSpeedBonus = 5f;
    19.         public float PowerUpDuration = 5f;
    20.         public float PowerUpCooldown = 10f;
    21.  
    22.         string MovementAxisName;
    23.         string TurnAxisName;
    24.         Rigidbody Rigidbody;
    25.         float MovementInputValue;
    26.         float TurnInputValue;
    27.         PowerUps PowerUp;
    28.  
    29.         void Awake ()
    30.         {
    31.             Rigidbody = GetComponent<Rigidbody> ();
    32.         }
    33.  
    34.         void OnEnable ()
    35.         {
    36.             Rigidbody.isKinematic = false;
    37.  
    38.             MovementInputValue = 0f;
    39.             TurnInputValue = 0f;
    40.         }
    41.  
    42.         void OnDisable ()
    43.         {
    44.             Rigidbody.isKinematic = true;
    45.         }
    46.  
    47.         void Start ()
    48.         {
    49.             MovementAxisName = "Vertical";
    50.             TurnAxisName = "Horizontal";
    51.         }
    52.  
    53.         void Update ()
    54.         {
    55.             MovementInputValue = Input.GetAxis (MovementAxisName);
    56.             TurnInputValue = Input.GetAxis (TurnAxisName);
    57.  
    58.             EngineAudio ();
    59.         }
    60.  
    61.         void EngineAudio ()
    62.         {
    63.             // If there is no input (the tank is stationary)...
    64.             if (Mathf.Abs (MovementInputValue) < 0.1f && Mathf.Abs (TurnInputValue) < 0.1f)
    65.             {
    66.                 // ... and if the audio source is currently playing the driving clip...
    67.                 if (MovementAudio.clip == EngineDriving)
    68.                 {
    69.                     // ... change the clip to idling and play it.
    70.                     MovementAudio.clip = EngineIdling;
    71.                     MovementAudio.Play ();
    72.                 }
    73.             }
    74.             else
    75.             {
    76.                 // Otherwise if the tank is moving and if the idling clip is currently playing...
    77.                 if (MovementAudio.clip == EngineIdling)
    78.                 {
    79.                     // ... change the clip to driving and play.
    80.                     MovementAudio.clip = EngineDriving;
    81.                     MovementAudio.Play();
    82.                 }
    83.             }
    84.         }
    85.  
    86.         void FixedUpdate ()
    87.         {
    88.             Move ();
    89.             Turn ();
    90.         }
    91.  
    92.         void Move ()
    93.         {
    94.             Vector3 movement = transform.forward * MovementInputValue * Speed * Time.deltaTime;
    95.  
    96.             Rigidbody.MovePosition (Rigidbody.position + movement);
    97.         }
    98.  
    99.         void Turn ()
    100.         {
    101.             float turn = TurnInputValue * TurnSpeed * Time.deltaTime;
    102.  
    103.             Quaternion turnRotation = Quaternion.Euler (0f, turn, 0f);
    104.  
    105.             Rigidbody.MoveRotation (Rigidbody.rotation * turnRotation);
    106.         }
    107.  
    108.         void OnTriggerEnter (Collider collider)
    109.         {
    110.             List<int> PowerUpsPossible = new List<int> ();
    111.  
    112.             if (collider.CompareTag("PowerUp") && PowerUp.isActivable_speed)
    113.             {
    114.                 PowerUpsPossible.Add (1);
    115.             }
    116.             if (collider.CompareTag("PowerUp") && PowerUp.isActivable_health)
    117.             {
    118.                 PowerUpsPossible.Add (2);
    119.             }
    120.             if (collider.CompareTag("PowerUp") && PowerUp.isActivable_damage)
    121.             {
    122.                 PowerUpsPossible.Add (3);
    123.             }
    124.             if (PowerUpsPossible.Count < 1)
    125.             {
    126.                 StartCoroutine(PowerUp.PowerUpMessage("ALL POWERUPS ON COOLDOWN", 4));
    127.             }
    128.  
    129.             int PowerUpRand = Random.Range (0, PowerUpsPossible.Count);
    130.  
    131.             switch (PowerUpsPossible [PowerUpRand])
    132.             {
    133.                 case 1:
    134.                     StartCoroutine (PowerUp.PlayerSpeedPowerUp());
    135.                     break;
    136.                 case 2:
    137.                     StartCoroutine (PowerUp.PlayerHealthPowerUp());
    138.                     break;
    139.                 case 3:
    140.                     StartCoroutine (PowerUp.PlayerDamagePowerUp());
    141.                     break;
    142.             }
    143.         }
    144.     }
    145. }
    146.  
    PowerUps.cs
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using PlayerInfo;
    5. using EnemyInfo;
    6. using Management;
    7. using UnityEngine.UI;
    8. using Shell;
    9.  
    10. public class PowerUps : MonoBehaviour
    11. {
    12.     public Text PowerUpMessageText;
    13.  
    14.     public float SpeedBonus, SpeedDuration, SpeedCooldown;
    15.     public float HealthPercentIncrease, HealthCooldown;
    16.     public float DamageMultiplier, DamageDuration, DamageCooldown;
    17.  
    18.     PlayerMovement pMove;
    19.     EnemyMovement eMove;
    20.     PlayerHealth pHealth;
    21.     EnemyHealth eHealth;
    22.     PlayerAttack pAttack;
    23.     EnemyAttack eAttack;
    24.     ShellExplosion peDamage;
    25.     float NewCurrentHealth;
    26.     static PowerUps instance;
    27.  
    28.     [HideInInspector] public bool isActivable_speed = true;
    29.     [HideInInspector] public bool isActivable_health = true;
    30.     [HideInInspector] public bool isActivable_damage = true;
    31.  
    32.     void Awake ()
    33.     {
    34.         instance = this;
    35.     }
    36.  
    37.     void Start ()
    38.     {
    39.         isActivable_speed = true;
    40.         isActivable_health = true;
    41.         isActivable_damage = true;
    42.     }
    43.  
    44.     public IEnumerator PowerUpMessage (string message, float Delay)
    45.     {
    46.         PowerUpMessageText.text = message;
    47.         PowerUpMessageText.enabled = true;
    48.         yield return new WaitForSeconds (Delay);
    49.         PowerUpMessageText.enabled = false;
    50.     }
    51.  
    52.     public IEnumerator PlayerSpeedPowerUp()
    53.     {
    54.         StartCoroutine(PowerUpMessage("SPEED POWERUP ACTIVATED! " + "+" + SpeedBonus + "SPEED FOR " + SpeedDuration + " SECONDS", 4));
    55.  
    56.         isActivable_speed = false;
    57.  
    58.         pMove.Speed += SpeedBonus;
    59.  
    60.         yield return new WaitForSeconds (SpeedDuration);
    61.  
    62.         pMove.Speed -= SpeedBonus;
    63.  
    64.         StartCoroutine (PowerUpMessage("SPEED POWERUP FINISHED! " + SpeedCooldown + " SECOND COOLDOWN", 4));
    65.  
    66.         yield return new WaitForSeconds (SpeedCooldown);
    67.  
    68.         isActivable_speed = true;
    69.     }
    70.  
    71.     public IEnumerator PlayerHealthPowerUp()
    72.     {
    73.         NewCurrentHealth = pHealth.CurrentHealth * HealthPercentIncrease;
    74.  
    75.         StartCoroutine (PowerUpMessage("HEALTH POWERUP USED! " + HealthPercentIncrease +  "% MORE HEALTH", 4));
    76.  
    77.         isActivable_health = false;
    78.  
    79.         pHealth.CurrentHealth += NewCurrentHealth;
    80.  
    81.         yield return new WaitForSeconds (HealthCooldown);
    82.  
    83.         isActivable_health = true;
    84.     }
    85.  
    86.     public IEnumerator PlayerDamagePowerUp()
    87.     {
    88.         float NewDamage = peDamage.MaxDamage * DamageMultiplier;
    89.  
    90.         StartCoroutine (PowerUpMessage ("DAMAGE POWERUP ACTIVATED! " + DamageMultiplier + "x HIGHER DAMAGE", 4));
    91.  
    92.         isActivable_damage = false;
    93.  
    94.         peDamage.MaxDamage += NewDamage;
    95.  
    96.         yield return new WaitForSeconds (DamageDuration);
    97.  
    98.         peDamage.MaxDamage -= NewDamage;
    99.  
    100.         StartCoroutine (PowerUpMessage ("DAMAGE POWERUP FINISH! " + DamageCooldown + " SECOND COOLDOWN", 4));
    101.  
    102.         yield return new WaitForSeconds (DamageCooldown);
    103.  
    104.         isActivable_damage = true;
    105.     }
    106. }
    107.  

    What am I doing wrong?
     
  2. luke_2

    luke_2

    Joined:
    Nov 20, 2012
    Posts:
    29
    As it says in the error:
    PlayerInfo.PlayerMovement.OnTriggerEnter(UnityEngine.Collider collider)(at Assets/Scripts/PlayerTank/PlayerMovement.cs:111)

    The problem is in line 111 of PlayerMovement.cs (or 112 of your listed code), the variable PowerUp is null (not set to anything).

    I'd guess PowerUps is a script attached to the PowerUp object, if so you'd need to change your code to something like (may be some typos, but roughly):

    if (collider.CompareTag("PowerUp"))
    {
    PowerUp=collider.GameObject.GetComponent<PowerUps>();
    if (PowerUp.isActivable_speed)
    {
    PowerUpsPossible.Add(1);​
    }​

    if (PowerUp.isActivable_damage)
    {​

    etc.....
     
  3. ltrout1999

    ltrout1999

    Joined:
    Oct 11, 2015
    Posts:
    125
    I changed the class to add this. Still same problem.
     
  4. luke_2

    luke_2

    Joined:
    Nov 20, 2012
    Posts:
    29
    Has the GameObject with the tag PowerUp got a PowerUps script attached to it?
     
  5. ltrout1999

    ltrout1999

    Joined:
    Oct 11, 2015
    Posts:
    125
    Yes. The powerup is activating whenever I go backwards onto the gameobect, but it's only showing the text and not actually affecting the speed, damage, etc.
     
  6. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    Have you checked if the object are set to trigger?
     
  7. luke_2

    luke_2

    Joined:
    Nov 20, 2012
    Posts:
    29
  8. ltrout1999

    ltrout1999

    Joined:
    Oct 11, 2015
    Posts:
    125
    They are set to trigger. I'll try the debugging real quick
     
  9. ltrout1999

    ltrout1999

    Joined:
    Oct 11, 2015
    Posts:
    125
    So the debugging was giving me problems so I decided to just mess around with the game itself and I noticed something. When the enemy shot me the shell collided with the player's tank's collider and popped up the message "ALL POWERUPS ON COOLDOWN" which was weird as I wasn't near the power up collider.