I'm working on a 2D space shooter game, and I made a powerup which is not working.
The Player's ship has 3 GameObjects attached to it, 3 guns. The powerUp script is storing gun configurations and when the PowerUp collides with the Player, the Player gets the gun configuration from the PowerUp, then destroys it.
The PowerUp is changing perodically, hence the CyclePowers()
procedure.
I store the configs in procedures that are executed on the creation of the PowerUp. They set the values of their appropriate classes.
The problem is, that a NullReferenceExeption is thrown when it tries to set the value.
Error is:
NullReferenceException: Object reference not set to an instance of an object PowerUp.SetupGunLaserSettings () (at Assets/PowerUp.cs:131) PowerUp.Start () (at Assets/PowerUp.cs:30)
I think that the problem is that the class does not exist when it tries to set its value. Why, I don't know.
Im a beginner in C#, so my assumption is probably wrong. Here is the code:
using UnityEngine;
using System.Collections;
public class PowerUp : MonoBehaviour {
// Use this for initialization
void Start () {
SetupGunLaserSettings();
SetupGunLaser2Settings();
powerType = powerCount;
InvokeRepeating("CyclePowers", 0.000001f, cycleRate);
}
public class gunSettings{
public string name;
//ETC
}
public class gunS{
public gunSettings gunSNose = new gunSettings();
//ETC
}
private gunS[] gunLaser = new gunS[3]; //powerType '0'
void SetupGunLaserSettings(){
for (int i = 0; i <= 2; i++) {
gunLaser[i].gunSNose.weaponActive = true; //This is where the exception is thrown.
//ETC
}
}
}