Private fields never assigned to?

Hello! I’m having some trouble assigning some values to my string arrays (attacks and output). I set-up my arrays in the beginning of the class and then populate these using method calls from start(). My issue is that when I go to debug Unity says that these arrays are never populated (even though I’ve called the relevant methods in start). I can’t see where I’m going wrong with this (heh, probably shouldn’t be doing this after a full day at work). Here’s my code:

using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;

public class RandomAtk : MonoBehaviour
{
    private System.Random randomGen;
    private string[] attacks;
    private string[] output;
    private Text attackOutput;

    // Use this for initialization
    void Start ()
    {
        randomGen = new System.Random ();
        attackOutput = GameObject.Find ("Attack Display").GetComponent<Text>();
        populateAtks ();
        generateAtks ();
    }
   
    // Update is called once per frame
    void Update ()
    {
   
    }

    //Populates the attack array
    void populateAtks()
    {
        attacks [0] = "->";
        attacks [1] = "<-";
        attacks [2] = "^";
        attacks [3] = "v";
        attacks [4] = "z";
        attacks [5] = "x";
        attacks [6] = "c";
    }

    //Responsible for generating what attacks should be displayed
    void generateAtks()
    {
        for (int i = 0; i < 6; i++)
        {
            output[i] = attacks[randomGen.Next(0,6)];
        }
    }

    void changeText()
    {
        attackOutput.text = output.ToString ();
    }

}

Any help would be greatly appreciated!

You can’t assign values to an array without first creating the array in memory. Otherwise it’s just a reference to an array that doesn’t exist in memory.

attacks = new string[7];
output = new string[6];
2 Likes

Absolutely correct, had a feeling it might have been something simple - many thanks!