0

NullReferenceException: Object reference not set to an instance of an object Tower.OnGUI () (at Assets/Tower.cs:100)

Relevant line is:

if(Main.Gold >= Towers.u[stage])

The variables in Towers are defined like so, am I doing it wrong?

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Towers : MonoBehaviour {

    public static float[] d;
    public static float[] r;
    public static float[] s;
    public static float[] u;

    // Use this for initialization
    void Start () { 
        d = new float[10];
        d[0] = 1f;
        d[1] = 3f;
        d[2] = 5f;
        d[3] = 7f;
        d[4] = 9f;
        d[5] = 11f;
        d[6] = 13f;
        d[7] = 15f;
        d[8] = 18f;
        d[9] = 21f;
        d[10] = 23f;        

        r = new float[10];
        r[0] = 5f;
        r[1] = 9f;
        r[2] = 13f;
        r[3] = 17f;
        r[4] = 21f;
        r[5] = 25f;
        r[6] = 29f;
        r[7] = 33f;
        r[8] = 37f;
        r[9] = 41f;
        r[10] = 45f;

        s = new float[10];
        s[0] = 3f;
        s[1] = 2.8f;
        s[2] = 2.6f;
        s[3] = 2.4f;
        s[4] = 2.2f;
        s[5] = 2f;
        s[6] = 1.8f;
        s[7] = 1.6f;
        s[8] = 1.4f;
        s[9] = 1.2f;
        s[10] = 1f;

        u = new float[10];
        u[0] = 50f;
        u[1] = 100f;
        u[2] = 150f;
        u[3] = 200f;
        u[4] = 250f;
        u[5] = 300f;
        u[6] = 350f;
        u[7] = 400f;
        u[8] = 450f;
        u[9] = 500f;
        u[10] = 0f;     
    }
}

Thanks!

Steven
  • 166,672
  • 24
  • 332
  • 435
Ivatrix
  • 31
  • 1
  • 11

3 Answers3

0

It seems that either Main is null or you didn't call Towers.Start() before the line causing the problem.

When initialising static fields like that, it can sometimes be better to use static initialisation where you declare the fields, by calling a private static method that returns the initialisation data.

Doing that means that you don't need a separate Start() method that you must remember to call.

For example:

public class Towers: MonoBehaviour
{
    public static float[] d = initD();
    public static float[] r = initR();
    public static float[] s = initS();
    public static float[] u = initU();

    private static float[] initD()
    {
        return new []
        {
             1f,
             3f,
             5f,
             7f,
             9f,
            11f,
            13f,
            15f,
            18f,
            21f,
            23f
        };
    }

    private static float[] initR()
    {
        return new []
        {
             5f,
             9f,
            13f,
            17f,
            21f,
            25f,
            29f,
            33f,
            37f,
            41f,
            45f
        };
    }

    private static float[] initS()
    {
        return new []
        {
            3.0f,
            2.8f,
            2.6f,
            2.4f,
            2.2f,
            2.0f,
            1.8f,
            1.6f,
            1.4f,
            1.2f,
            1.0f
        };
    }

    private static float[] initU()
    {
        return new[]
        {
             50f,
            100f,
            150f,
            200f,
            250f,
            300f,
            350f,
            400f,
            450f,
            500f,
              0f
        };
    }
}
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
0

You should place initialization of any mono behaviours inside Awake instead of Start, Awake is the 1st event you can capture in the MonoBehaviour's life cycle.

Edit: Keep in mind that if you try to access other MonoBehaviours from Awake, you can't know for sure that they were initialized already.

Ron
  • 1,806
  • 3
  • 18
  • 31
0

You should move lines in Start function to the Awake function which is called when creating. And you tried to access the array from 0 to 10, but the array size is 10, so you have to access the array up to 9. If you want to access tenth value of the array, you have to allocate it as 11 size which means actual count of the values.

sonee
  • 1
  • 1