Why is Random.Range not working in array

Hey!

I’m working on entering random names to make an NPCs full name. but the random.range isn’t working and the debug.logs print nothing.

the next script is where i use Random.range, but i’m not sure if i’m doing something wrong.


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "New Character", menuName = "create Character")]
public class Names : MonoBehaviour
{
    //THIS IS THE CLASS WITH ALL THE NAMES
    public static Names Instance;

    void Awake()
    {
        Instance = this;
    }

    public string[] gender = new string[] { "Female", "Male" };

    public string[] status = new string[] { "Married", "Single", "Divorced", "Widow", "Seperated", "Open Relationship" };


    public string[] femaleFN = new string[] { "Isobelle", "Katy", "Eliza", "Beth", "Salma", "Verity", "Ayla", "Jodie", "Alice",
        "Ali", "Pippa", "Zoe", "Brianna", "Bea", "Isabelle", "Nadia", "Esther", "Essie", "Crystal", "Amina", "Nora", "Bethany",
        "Emily", "Emmy", "Paula", "Zara", "Jose", "Lena", "Yasmine", "Ria", "Scarlett", "Evie", "Joanna", "Joann", "Amber", "Margaret",
        "Meg", "Alyssa", "Maria", "Mimi", "Josephine", "Josette", "Hermione", "Julia", "Jules", "Chantelle", "Layla", "Kian", "Agnes",
        "Nessa", "Courtney", "Kyla", "Katherine", "Kaira", "Claudia", "Harriet", "Hennie", "Mariam", "Mitzi", "Jessie", "Alex", "Lexi",
        "Margie", "Lana", "Taylor", "Katie", "Cate", "Saskia", "Alicia", "Aidan", "Alannah", "Mollie", "Beatrice", "Madeleine", "Lottie",
        "Rhonda", "Mae", "Willie", "Molly", "Faith", "Amira", "Frances", "Fanny", "Kathleen", "Kathy", "Ida", "Michaela", "Michal", "Ana",
        "Alana", "Stacey", "Nastia", "Jemima", "Melanie", "Autumn", "Rachael", "Zainab", "Meghan", "Heidi", "Addie", "Jamie", "Francesca",
        "Franki", "Fiona", "Edie", "Aminah", "Beth", "Lise", "Orla", "Tyler", "Edith", "Flora", "Maddie", "Tallulah", "Holly", "Tabitha",
        "Max", "Harley", "Morgan", "Lia", "Linda", "Lindy", "Lachlan", "Talia" };

    public string[] maleFN = new string[] { "Adam", "Alex", "Aaron", "Ben", "Carl", "Dan", "David", "Edward", "Fred", "Frank",
        "George", "Hal", "Hank", "Ike", "John", "Jack", "Joe", "Larry", "Monte", "Matthew", "Mark", "Nathan", "Otto", "Paul",
        "Peter", "Roger", "Roger", "Steve", "Thomas", "Tim", "Ty", "Victor", "Walter", "Cruz", "Jaylon", "Brian", "Wade", "Jayson",
        "Deegan", "Prince", "London", "Greyson", "Jeffrey", "Mekhi", "Neil", "Pablo", "Spencer", "Gilbert", "Desmond", "Moshe", "Gage",
        "Charles", "Tristan", "Tanner", "Brady", "Kameron", "Brett", "Mauricio", "Max", "Aditya", "Gregory", "Kamron", "Seth", "Clay",
        "Grayson", "Reilly", "Juan", "Finn", "Santos", "Quincy", "Corey", "Wyatt", "Henry", "Emiliano", "Dillan", "Elisha", "Yair", "Xavier",
        "Vicente", "Erick", "Marcus", "Cordell", "Odin" };

    public string[] lastName = new string[] { "Anderson", "Ashwoon", "Aikin", "Bateman", "Bongard", "Bowers", "Boyd", "Cannon", "Cast",
        "Deitz", "Dewalt", "Ebner", "Frick", "Hancock", "Haworth", "Hesch", "Hoffman", "Kassing", "Knutson", "Lawless", "Lawicki",
        "Mccord", "McCormack", "Miller", "Myers", "Nugent", "Ortiz", "Orwig", "Ory", "Paiser", "Pak", "Pettigrew", "Quinn", "Quizoz",
        "Ramachandran", "Resnick", "Sagar", "Schickowski", "Schiebel", "Sellon", "Severson", "Shaffer", "Solberg", "Soloman", "Sonderling",
        "Soukup", "Soulis", "Stahl", "Sweeney", "Tandy", "Trebil", "Trusela", "Trussel", "Turco", "Uddin", "Uflan", "Ulrich", "Upson", "Vader",
        "Vail", "Valente", "Van Zandt", "Vanderpoel", "Ventotla", "Vogal", "Wagle", "Wagner", "Wakefield", "Weinstein", "Weiss", "Woo", "Yang",
        "Yates", "Yocum", "Zeaser", "Zeller", "Ziegler", "Bauer", "Baxster", "Casal", "Cataldi", "Caswell", "Celedon", "Chambers", "Chapman",
        "Christensen", "Darnell", "Davidson", "Davis", "DeLorenzo", "Dinkins", "Doran", "Dugelman", "Dugan", "Duffman", "Eastman", "Ferro",
        "Ferry", "Fletcher", "Fietzer", "Hylan", "Hydinger", "Illingsworth", "Ingram", "Irwin", "Jagtap", "Jenson", "Johnson", "Johnsen", "Jones",
        "Jurgenson", "Kalleg", "Kaskel", "Keller", "Leisinger", "LePage", "Lewis", "Linde", "Lulloff", "Maki", "Martin", "McGinnis", "Mills", "Moody",
        "Moore", "Napier", "Nelson", "Norquist", "Nuttle", "Olson", "Ostrander", "Reamer", "Reardon", "Reyes", "Rice", "Ripka", "Roberts", "Rogers", "Root",
        "Sandstrom", "Sawyer", "Schlicht", "Schmitt", "Schwager", "Schutz", "Schuster", "Tapia", "Thompson", "Tiernan", "Tisler" };


}

        //NPC PROFILE SETUP
        npcProfile.gender = Names.Instance.gender[Random.Range(0, Names.Instance.gender.Length)];
        Debug.Log("gender is " +npcProfile.gender);

        if (npcProfile.gender == "Female")
        {
            Debug.Log("hello");

            string fN = "sis";//Names.Instance.femaleFN[Random.Range(0, Names.Instance.femaleFN.Length)];
            string lN = Names.Instance.lastName[Random.Range(0, Names.Instance.lastName.Length)];
            npcProfile.name = fN + " " ;
            Debug.Log(fN);
            npcProfile.status = Names.Instance.status[Random.Range(0, Names.Instance.status.Length)];
            npcProfile.occupation = Occupations.Instance.jobs[Random.Range(0, Occupations.Instance.jobs.Length)];
            npcProfile.bankBalance = (int)Random.Range(0, 999999999);
            npcProfile.age = (int)Random.Range(0, 100);
            //apperance
            npcProfile.happiness = Random.Range(0, 100);
            npcProfile.energy = Random.Range(0, 100);
            npcProfile.looks = Random.Range(0, 100);
            npcProfile.smarts = Random.Range(0, 100);
        }
		else
		{
            string fN = "brah";//Names.Instance.maleFN[Random.Range(0, Names.Instance.maleFN.Length)];
            string lN = Names.Instance.lastName[Random.Range(0, Names.Instance.lastName.Length)];
            npcProfile.name = fN + " " + lN;
            Debug.Log(npcProfile.name);
            npcProfile.status = Names.Instance.status[Random.Range(0, Names.Instance.status.Length)];
            npcProfile.occupation = Occupations.Instance.jobs[Random.Range(0, Occupations.Instance.jobs.Length)];
            npcProfile.bankBalance = (int)Random.Range(0, 999999999);
            npcProfile.age = (int)Random.Range(0, 100);
            //apperance
            npcProfile.happiness = Random.Range(0, 100);
            npcProfile.energy = Random.Range(0, 100);
            npcProfile.looks = Random.Range(0, 100);
            npcProfile.smarts = Random.Range(0, 100);
        }

My guess is that because you declared your arrays public and they are part of a monobehaviour the variables actually got serialized while they were initially empty. Changing the field initializer value in the script has no effect once you have the serialized values in the inspector as the serialized values will overwrite whatever you initialized the variables to.

Since you use hardcoded arrays of names you probably don’t want those arrays to be serialized at all. I don’t quite understand why your “Names” class is actually a MonoBehaviour in the first place. Also the CreateAssetMenu has no effect for MonoBehaviour classes.

I see several options here. If you want to keep your name lists hardcoded in code you should either use a plain C# singleton and not derive from MonoBehaviour or ScriptableObject. The second option is to turn all your hardcoded arrays into public static readonly fields inside an ordinary C# class (again, no MonoBehaviour or ScriptableObject).

However if you actually want to be able to edit / enter the names in the inspector you shouldn’t hardcode them in the first place. Just edit / add them in the inspector of your instance.

Commonly such lists of names are usually outsourced into a seperate config file and read at start. In this case it’s probably the simplest solution to just use 3 text files and use 3 TextAsset references to those files. In Awake you can simply read the “text” property of those text assets and split it at the new line character to get an array of all the names in the file. However if you have more data than those 3 lists using json or xml is usually a better approach.

If you REALLY want to keep the MonoBehaviour singleton and keep your names hardcoded you can add the [NonSerialized] attribute before all your array declarations to prevent Unity from serializing the arrays.

Finally if you don’t add the NonSerialized attribute you can also right click on your variables in the inspector and selecting “Reset”. This should reset the serialized data to the initial data. However this is just a one time solution. The actual lists would still be serialized and changes to the code would not affect the actual lists unless you reset it again. This is of course counter productive, misleading and just a pain to work with.