- Home /
Resizable Array of Classes?
Hopefully I'm using the right terminology here.
So I'm using JavaScript and I need to create a resizable array that contains a class.
I started with an array that's visible in the inspector to make sure all my logic was working, and everything works fine. The only downside is that I need to pre-set the number of elements for each array in the inspector in order to avoid "Out Of Range" errors. Considering there's going to be well over 100 "Missions" each with a range of objectives I really don't want to enter all this information manually.
So I need the arrays to be resizable and I'm not quite sure how to do that. I'm familiar with regular resizable arrays, just not how to integrate them with a class.
This is the structure using built-in arrays and it works very well:
var missions : Missions[];
class Missions {
var missionTitle : String;
var objectives : String[];
var missionTime : float;
}
So now I need to convert that into a resizable array so that I can easily change the number of missions and the number of objectives. Any suggestions?
Thanks!
Answer by robhuhn · Feb 04, 2014 at 02:36 PM
What about using Lists in UnityScript?
import System.Collections.Generic;
var missions : List.<Missions> = List.<Missions>();
class Missions {
var missionTitle : String;
var objectives : List.<String> = List.<String>();
var missionTime : float;
}
And then add or remove objectives and missions
missions.Add(mission);
missions.Remove(mission);
mission.objectives.get(0);
Thank you for your answer. This definitely works too, but I've got to accept Nick's answer since he commented first. :D
Your answer
Follow this Question
Related Questions
Class Object Array? 1 Answer
C# Array of Child Objects 1 Answer
Add custom declared variables in a Array 1 Answer
Prepopulate Class Array 0 Answers
Cannot edit values of Class when created using .JSON file 1 Answer