Creating a "roster" of Randomly generated characters.

First, I’m fairly new to scripting/programming.

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

Best way will be to use template class like so:

public class SportMan{
string Name;
int Stamina;
//etc.

public void SportMan(string name, int stamina){
Name = name;
Stamina = stamina;
}

}

And a list:

List<SportMan> SportGuys = new List<SportMan>();
//And You can add Your sport man like so
SportGuys.Add(new SportMan("RandomName", RandomStamina));

Then You can use list to store and populate whatever You like and all informations about each athlete will be there.

1 Like

Thanks alot, this gives me a good start point. I already see how it will work.

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));
}
}

Could You show errors from console cause it is hard to estimate where error is coming from.

Here’s the error:
NullReferenceException: Object reference not set to an instance of an object
Generator.Start () (at Assets/TEST/Generator.cs:18)

_pos is never set to anything… and then you try to access it’s attributes

I thought this was what I was trying to do with the line:

_newAthlete.AthletePos = new GuardPos()

no, _pos and _newAthlete.AthletePos are entirely different things. If you want to use _newAthlete.AthletePos then tell it to use that not _pos.

whilst true, scratch that… you don’t want to add a new object at all, you want to add the object you just created, so it should be

_freeAgents.Add(_newAthlete);