Modify Custom Array Data
Good morning all,
I come to you today with an issue i'm hoping you can help with. Before we start i'd like to say that i've genuinely tried to fix this issue on my own but i just can't get it. So i have a main script named "MainButton" and within it i have this method
public void TrainTroops (int NumberToTrain, int InterTroopID)
{
CurrentTroops = PlayerPrefs.GetInt(TroopManager.Instance.Troop_Types[InterTroopID].SaveDataName);
ManageCurrency(TroopManager.Instance.Troop_Types[InterTroopID].TroopPrice, true);
PlayerPrefs.SetInt(TroopManager.Instance.Troop_Types[InterTroopID].SaveDataName, CurrentTroops += NumberToTrain);
UpdateTroopCounts();
}
The above method is called when i button is pressed on. It's getting it's variables from another class which can be seen below;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TroopManager : MonoBehaviour {
public static TroopManager Instance { get; }
public TroopTypes[] Troop_Types = new TroopTypes[2];
// Use this for initialization
void Start () {
// TroopTypes[] Troop_Types = new TroopTypes[2];
Troop_Types = new TroopTypes[2];
//Create Troops Array (ARRAY))
Troop_Types[0].TroopDamage = 12;
Troop_Types[0].TroopPrice = 20;
Troop_Types[0].TroopName = "Mer-Tany";
Troop_Types[0].TroopID = 0;
Troop_Types[0].SaveDataName = "player_mert_count";
Troop_Types[1].TroopDamage = 15;
Troop_Types[1].TroopPrice = 20;
Troop_Types[1].TroopName = "Knight";
Troop_Types[1].TroopID = 1;
Troop_Types[1].SaveDataName = "player_knight_count";
Debug.Log("Troop Manager initilized");
}
// Update is called once per frame
void Update () {
}
}
The above class is where the issue starts i believe as when executed their are no errors displayed in the console but it also doesn't print my debug line either. This class is creating an array using a type i created and is assigning them values. The type can be seen below
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TroopTypes {
//define all of the values for the class
public int TroopHealth;
public string TroopName;
public int TroopPrice;
public int TroopDamage;
public int TroopID;
public string SaveDataName;
//define a constructor for the class
public TroopTypes()
{
}
}
So after showing you guys all that; let me explain the issue, when the button is clicked in the MainButton class to call the Method giving me problems i get a NullPointerException as seen below; Could you guys please take a look as i'm at a complete loss. Thanks everyone
Your answer
Follow this Question
Related Questions
null array? 0 Answers
how do i create a array eg imBank(11) of type mytype: 2 Answers
No Vector3Int on version 5.6.5p1 2 Answers
C# ArrayList Accessing and RemoveAt? 0 Answers
Another Physics.OverlapSphere question 0 Answers