I’m trying to build some sorts of sports manager. I’ve started messing with scripts to generate one random athlete. That seems to work fine. Now I want this script to generate 10 (or 100) athlete and save each of them somewhere I could access their variable. Then I’ll look into assigning these athlete to different teams (container I guess?).
I have no idea where to start. Should I make my Generator script make each newly created athlete a new game object? Save it and then store these athlete in a “Free Agent” Array? It seems a bit overkill and messy. I’ve look at the training on serialization and I see how it will work with the teams and free agent, but I’m kind of stuck at the athlete generation.
Any advice, tips or event start of a solution?
Thanks
I keep gettinf a NullReferenceException: Object reference not set to an instance of an object. When adding the new BaseAthlete int he list. Here’s the classes:
public class BaseAthlete
{
private BasePos _athletePos;
private int _athleteStrength = 0;
private int _athleteAgility = 0;
public BasePos AthletePos { get; set; }
public string AthletePosName { get; set; }
public int AthleteStrength { get; set; }
public int AthleteAgility { get; set; }
public BaseAthlete(string name, int str, int agil){
AthletePosName = name;
AthleteStrength = str;
AthleteAgility = agil;
}
}
Then Base Position, because stats Range depend on position
using System.Collections;
using UnityEngine;
public class BasePos
{
private string _athletePosName;
private int _strength;
private int _agility;
public string PosName { get; set; }
public int Strength { get; set; }
public int Agility { get; set; }
}
My position class looks like this:
public class GuardPos : BasePos {
public GuardPos()
{
PosName = "Guard";
Strength = UnityEngine.Random.Range (8, 10);
Agility = UnityEngine.Random.Range (15, 20);
}
}
And finally the generator
private BaseAthlete _newAthlete;
private BasePos _pos;
private int _choosePos = 0;
public List <BaseAthlete> _freeAgents = new List<BaseAthlete>();
void Start()
{
_newAthlete = new BaseAthlete ("", 0, 0);
_choosePos = UnityEngine.Random.Range (1, 3);
if (_choosePos == 1) {
_newAthlete.AthletePos = new GuardPos ();
}
if (_choosePos == 2) {
_newAthlete.AthletePos = new ForwardPos ();
}
_freeAgents.Add (new BaseAthlete (_pos.PosName, _pos.Strength, _pos.Agility));
}
}