Alright guys, I’ve asked for a little bit of help from the users of the forum earlier to help me figure out a good way to use this system and the idea that I was thinking about using was recommended, although I just can’t figure out HOW to do it.
This checks to see if the unit is inside of a rectangle created by holding the mouse down, this allows me to set the “Selected” Boolean of numerous units to true at a single time.
I’m trying to figure out a way to store some values of the units that are selected.
After posting and asking for opinions and doing some research, it seems like a List would be the way to go, although I have no idea how to go about it… I need the Name(String) and the unitID(Integer) values stored from the Unit script. (Attached to every script that has the Selectable script attached)
Once these values are stored, I’ll be the happiest person in the world. I’ve found myself on a pretty steep learning curve for my first two weeks in unity, but I feel as if I’ve progressed a great deal, but I couldn’t have done it without your guys help. Once I have this system done, I’ll show some progress on what I’ve been working on.
Sounds like you need a list of selected Unit objects. I wouldn’t store the name and id from the Unit when you can just store the Unit directly, which gives you access to anything else you need to know about each Unit (which you may find you need as time goes by).
// Do this once on startup
List<Unit> selectedUnits = new List<Unit>();
// Clear list if already populated previously
selectedUnits.Clear();
// Loop through all units looking for selected ones
foreach(var unit in allUnits)
{
if(unit.isSelected) // or however you flag a unit as being selected
{
selectedUnits.Add(unit);
}
}
// Now you have a list of selected units.
Could you explain how this works for me? Does this take the “Unit” script and store all of the data inside of it into a list? or is there a Unit class in unity I’m missing somewhere? As far as I’m concerned I haven’t done anything special and everything is using MonoBehaviour, so I haven’t created my own unit class.
I have assumed that (if using C#) you have a file named Unit.cs (your script) and in that file you have defined a public class named Unit (that possibly derives from MonoBehaviour).
Classes are reference types so you are only ever storing a reference to it (in lists, etc).