- Home /
Access list storing custom class variables from another script
My first script looks like this: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class UpgradeManager : MonoBehaviour {
public static List<Upgrades> UpgradesList = new List<Upgrades>();
public class Upgrades
{
public string UpgradeName;
public int MultiplierNumber;
public double UpgradePrice;
public int TargetGenerator;
public Upgrades(string newName, int newNumber, double newPrice, int newGenerator)
{
UpgradeName = newName;
MultiplierNumber = newNumber;
UpgradePrice = newPrice;
TargetGenerator = newGenerator;
}
}
public Upgrades Upgrade1 = new Upgrades("2x infrastructure", 2, 100, 1);
public Upgrades Upgrade2 = new Upgrades("3x Tax rate", 3, 150, 0);
public Upgrades Upgrade3 = new Upgrades("4x infrastrucutre", 4, 200, 1);
private void Start()
{
UpgradesList.Add(Upgrade1);
UpgradesList.Add(Upgrade2);
UpgradesList.Add(Upgrade3);
}
}
And my second script tries to retrieve data from this script like this: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class UpgradeScript : MonoBehaviour { public string UpgradeName = UpgradeManager.UpgradesList[0].UpgradeName; public int MultiplierNumber = UpgradeManager.UpgradesList[0].MultiplierNumber; public double UpgradePrice = UpgradeManager.UpgradesList[0].UpgradePrice; public int TargetGenerator = UpgradeManager.UpgradesList[0].TargetGenerator;
void Update()
{
}
}
And when I try to run my game it gives me this error: ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Somebody please help me thank you.
Answer by unity_ek98vnTRplGj8Q · Dec 30, 2019 at 05:22 PM
The problem is you are trying to access the list before any items have been added to it. In your first script, move the code in the Start
function to the Awake
function. Then in your second script, initialize your values in the Start
function.
public class UpgradeManager : MonoBehaviour {
public static List<Upgrades> UpgradesList = new List<Upgrades> ();
public class Upgrades {
public string UpgradeName;
public int MultiplierNumber;
public double UpgradePrice;
public int TargetGenerator;
public Upgrades (string newName, int newNumber, double newPrice, int newGenerator) {
UpgradeName = newName;
MultiplierNumber = newNumber;
UpgradePrice = newPrice;
TargetGenerator = newGenerator;
}
}
public Upgrades Upgrade1 = new Upgrades ("2x infrastructure", 2, 100, 1);
public Upgrades Upgrade2 = new Upgrades ("3x Tax rate", 3, 150, 0);
public Upgrades Upgrade3 = new Upgrades ("4x infrastrucutre", 4, 200, 1);
private void Awake() {
UpgradesList.Add (Upgrade1);
UpgradesList.Add (Upgrade2);
UpgradesList.Add (Upgrade3);
}
}
and
public class UpgradeScript : MonoBehaviour {
public string UpgradeName;
public int MultiplierNumber;
public double UpgradePrice;
public int TargetGenerator;
void Start () {
UpgradeName = UpgradeManager.UpgradesList[0].UpgradeName;
MultiplierNumber = UpgradeManager.UpgradesList[0].MultiplierNumber;
UpgradePrice = UpgradeManager.UpgradesList[0].UpgradePrice;
int TargetGenerator = UpgradeManager.UpgradesList[0].TargetGenerator;
}
}
Sorry for my previous comment it just worked i forgot so save my script!!!! Thank you so much!!!!
Your answer
Follow this Question
Related Questions
Why the List in the class when using it is not the same in another class ? 1 Answer
instantiating unique classes of a type 0 Answers
Return a gameObject from LINQ .GroupBy() 1 Answer
Need my function to work with different lists of different values (classes) 1 Answer
Sorting a list based of a variable 1 Answer