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:
NullReferenceException: Object reference not set to an instance of an object
PlayerInfo.PlayerMovement.OnTriggerEnter (UnityEngine.Collider collider) (at Assets/Scripts/PlayerTank/PlayerMovement.cs:111)
Here are my classes that are handling the Power Ups:
PlayerMovement.cs
using UnityEngine;
using System.Collections;
using EnemyInfo;
using Shell;
using UnityEngine.UI;
using System.Collections.Generic;
namespace PlayerInfo
{
public class PlayerMovement : MonoBehaviour
{
public float Speed = 10f;
public float TurnSpeed = 180f;
public AudioSource MovementAudio;
public AudioClip EngineIdling;
public AudioClip EngineDriving;
public float PowerUpSpeedBonus = 5f;
public float PowerUpDuration = 5f;
public float PowerUpCooldown = 10f;
string MovementAxisName;
string TurnAxisName;
Rigidbody Rigidbody;
float MovementInputValue;
float TurnInputValue;
PowerUps PowerUp;
void Awake ()
{
Rigidbody = GetComponent<Rigidbody> ();
}
void OnEnable ()
{
Rigidbody.isKinematic = false;
MovementInputValue = 0f;
TurnInputValue = 0f;
}
void OnDisable ()
{
Rigidbody.isKinematic = true;
}
void Start ()
{
MovementAxisName = "Vertical";
TurnAxisName = "Horizontal";
}
void Update ()
{
MovementInputValue = Input.GetAxis (MovementAxisName);
TurnInputValue = Input.GetAxis (TurnAxisName);
EngineAudio ();
}
void EngineAudio ()
{
// If there is no input (the tank is stationary)...
if (Mathf.Abs (MovementInputValue) < 0.1f && Mathf.Abs (TurnInputValue) < 0.1f)
{
// ... and if the audio source is currently playing the driving clip...
if (MovementAudio.clip == EngineDriving)
{
// ... change the clip to idling and play it.
MovementAudio.clip = EngineIdling;
MovementAudio.Play ();
}
}
else
{
// Otherwise if the tank is moving and if the idling clip is currently playing...
if (MovementAudio.clip == EngineIdling)
{
// ... change the clip to driving and play.
MovementAudio.clip = EngineDriving;
MovementAudio.Play();
}
}
}
void FixedUpdate ()
{
Move ();
Turn ();
}
void Move ()
{
Vector3 movement = transform.forward * MovementInputValue * Speed * Time.deltaTime;
Rigidbody.MovePosition (Rigidbody.position + movement);
}
void Turn ()
{
float turn = TurnInputValue * TurnSpeed * Time.deltaTime;
Quaternion turnRotation = Quaternion.Euler (0f, turn, 0f);
Rigidbody.MoveRotation (Rigidbody.rotation * turnRotation);
}
void OnTriggerEnter (Collider collider)
{
List<int> PowerUpsPossible = new List<int> ();
if (collider.CompareTag("PowerUp") && PowerUp.isActivable_speed)
{
PowerUpsPossible.Add (1);
}
if (collider.CompareTag("PowerUp") && PowerUp.isActivable_health)
{
PowerUpsPossible.Add (2);
}
if (collider.CompareTag("PowerUp") && PowerUp.isActivable_damage)
{
PowerUpsPossible.Add (3);
}
if (PowerUpsPossible.Count < 1)
{
StartCoroutine(PowerUp.PowerUpMessage("ALL POWERUPS ON COOLDOWN", 4));
}
int PowerUpRand = Random.Range (0, PowerUpsPossible.Count);
switch (PowerUpsPossible [PowerUpRand])
{
case 1:
StartCoroutine (PowerUp.PlayerSpeedPowerUp());
break;
case 2:
StartCoroutine (PowerUp.PlayerHealthPowerUp());
break;
case 3:
StartCoroutine (PowerUp.PlayerDamagePowerUp());
break;
}
}
}
}
PowerUps.cs
using UnityEngine;
using System.Collections;
using PlayerInfo;
using EnemyInfo;
using Management;
using UnityEngine.UI;
using Shell;
public class PowerUps : MonoBehaviour
{
public Text PowerUpMessageText;
public float SpeedBonus, SpeedDuration, SpeedCooldown;
public float HealthPercentIncrease, HealthCooldown;
public float DamageMultiplier, DamageDuration, DamageCooldown;
PlayerMovement pMove;
EnemyMovement eMove;
PlayerHealth pHealth;
EnemyHealth eHealth;
PlayerAttack pAttack;
EnemyAttack eAttack;
ShellExplosion peDamage;
float NewCurrentHealth;
static PowerUps instance;
[HideInInspector] public bool isActivable_speed = true;
[HideInInspector] public bool isActivable_health = true;
[HideInInspector] public bool isActivable_damage = true;
void Awake ()
{
instance = this;
}
void Start ()
{
isActivable_speed = true;
isActivable_health = true;
isActivable_damage = true;
}
public IEnumerator PowerUpMessage (string message, float Delay)
{
PowerUpMessageText.text = message;
PowerUpMessageText.enabled = true;
yield return new WaitForSeconds (Delay);
PowerUpMessageText.enabled = false;
}
public IEnumerator PlayerSpeedPowerUp()
{
StartCoroutine(PowerUpMessage("SPEED POWERUP ACTIVATED! " + "+" + SpeedBonus + "SPEED FOR " + SpeedDuration + " SECONDS", 4));
isActivable_speed = false;
pMove.Speed += SpeedBonus;
yield return new WaitForSeconds (SpeedDuration);
pMove.Speed -= SpeedBonus;
StartCoroutine (PowerUpMessage("SPEED POWERUP FINISHED! " + SpeedCooldown + " SECOND COOLDOWN", 4));
yield return new WaitForSeconds (SpeedCooldown);
isActivable_speed = true;
}
public IEnumerator PlayerHealthPowerUp()
{
NewCurrentHealth = pHealth.CurrentHealth * HealthPercentIncrease;
StartCoroutine (PowerUpMessage("HEALTH POWERUP USED! " + HealthPercentIncrease + "% MORE HEALTH", 4));
isActivable_health = false;
pHealth.CurrentHealth += NewCurrentHealth;
yield return new WaitForSeconds (HealthCooldown);
isActivable_health = true;
}
public IEnumerator PlayerDamagePowerUp()
{
float NewDamage = peDamage.MaxDamage * DamageMultiplier;
StartCoroutine (PowerUpMessage ("DAMAGE POWERUP ACTIVATED! " + DamageMultiplier + "x HIGHER DAMAGE", 4));
isActivable_damage = false;
peDamage.MaxDamage += NewDamage;
yield return new WaitForSeconds (DamageDuration);
peDamage.MaxDamage -= NewDamage;
StartCoroutine (PowerUpMessage ("DAMAGE POWERUP FINISH! " + DamageCooldown + " SECOND COOLDOWN", 4));
yield return new WaitForSeconds (DamageCooldown);
isActivable_damage = true;
}
}
What am I doing wrong?