-1

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               
        }
    }

}
  • 1
    Please reduce this to a [mcve]. There's a *lot* of code here, almost all of which is probably irrelevant. But I don't see any code where you're *populating* the array. (You're never creating any instances of the `gunS` class.) – Jon Skeet Aug 15 '17 at 21:05
  • Please post a [Minimal, Complete, and a Verifiable](https://stackoverflow.com/help/mcve) example. In other words please narrow down your problem removing all the clutter. – Sach Aug 15 '17 at 21:05
  • At the top of your `for` loop you need to actually create the instances of the `gunS` class. Try adding this line just inside your `for` loop: `gunLaser[i] = new gunsS();` – Chris Dunaway Aug 15 '17 at 21:09

1 Answers1

0

You declare an array, but it is empty when you start setting properties on its members. Try creating your GunS items:

for (int i = 0; i <= 2; i++) 
{
    gunLaser[i] = new GunS();
    gunLaser[i].gunSNose.weaponActive = true; 

Try to put your declarations at the top of your class, by the way. That's where people will look for them. By scattering them through the code you confuse anyone who tries to understand your code :)

oerkelens
  • 4,600
  • 1
  • 19
  • 28
  • Thanks a lot! I thought that an array is created full with default values. Yea, I usually put my declarations at the top, I only did this to make the code easier to read and change for me :D – Argual Leothon Aug 15 '17 at 21:17
  • It is created full of default values, but in this case, that default value is `null`. If you create an array of `bool`, they will be `false` (but a bool?[] will contain `null`s!) – oerkelens Aug 15 '17 at 21:19