- Home /
This is not a Unity question, but a pure C#-related one, and thus, does not belong to Unity Answers.
Create Constructor Using Random List Elements?
Issue I'm trying to make a character generator using lists of attributes that I created. How do I do this? Goal To be able to call upon random elements from the list in order to generate a character when a button is pressed. What I Have Right now, I have a constructor for the character set up in one script and several lists set up in another. I'll include the constructor script and two of the lists to show what I'm working with so far.
public class ListSetUp : MonoBehaviour
{
public class Character
{
public string gender;
public string age;
public string style;
public string personality01;
public string personality02;
public string role;
public string race;
public string accessory;
public Character(string newGender, string newAge, string newStyle, string newPersonality01, string newPersonality02, string newRole, string newRace, string newAcessory)
{
gender = newGender;
age = newAge;
style = newStyle;
personality02 = newPersonality02;
personality01 = newPersonality01;
role = newRole;
race = newRace;
accessory = newAcessory;
}
void OnMouseDown()
{
}
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CharacterGenerator : MonoBehaviour {
public List<string> genders = new List<string>();
public List<string> ages = new List<string>();
void Awake()
{
///GenderListElements
genders.Add("Male");
genders.Add("Female");
genders.Add("Ambiguous");
///AgeListElements
ages.Add("Child");
ages.Add("Teen");
ages.Add("Adult");
ages.Add("Elder");
ages.Add("Ageless");
Answer by Runalotski · Aug 11, 2016 at 06:12 PM
You can make CharacterGenerator class 'static' and not inherit from monoBehaviour so it will become
public static class CharacterGenerator{
}
What this means is there is only one characterClass object so you can call it from anywhere
Because it no longer inherits from MonoBehaviour you cant use Awake so we will declare and initialise at the same time like this
public static List<string> genders = new List<string>{"Male", "Female", "Ambi"};
public static List<string> ages = new List<string>{"Child", "Teen"};
so now the class will look like this
public static class CharacterGenerator{
public static List<string> genders = new List<string>{"Male", "Female", "Ambi"};
public static List<string> ages = new List<string>{"Child", "Teen"};
}
now to use this in your script that would want to make a new character
Character newChar = new Character(CharacterGenerator.genders[randomNumber],
CharacterGenerator.ages [randomNumber])
Thank you for responding so quickly! I'll try this tonight and let you know if it works!
I ran into an issue. It says that randomNumber doesn't exist in the current context.
Character newChar = new Character(CharacterGenerator.genders[Random.Range(0, CharacterGenerator.genders.Length)],
CharacterGenerator.ages [Random.Range(0, CharacterGenerator.ages .Length)])
I changed .length to .count and that worked, but it's telling me that the character constructor needs two arguments.
@Runalotski Oh! Okay. This is what I have so far, including two lists.
using UnityEngine;
using System.Collections;
public class ListSetUp : $$anonymous$$onoBehaviour
{
public class Character
{
public string gender;
public string age;
public string style;
public string personality01;
public string personality02;
public string role;
public string race;
public string accessory;
Character newChar = new Character(CharacterGenerator.genders[Random.Range(0, CharacterGenerator.genders.Count)],
CharacterGenerator.ages[Random.Range(0, CharacterGenerator.ages.Count)]);
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public static class CharacterGenerator {
public static List<string> genders = new List<string> { "$$anonymous$$ale", "Female", "Ambiguous" };
public static List<string> ages = new List<string> { "Child", "Teen", "Adult", "Elder", "Ageless" };
Okay so I would say make each of the three objects a script in its own right so make a c# script for
Character and it will be this
using UnityEngine;
using System.Collections;
public class Character
{
//these are your variables that define a character
public string name;
public string gender;
// this is constructor and is called when you use the new keyword
public Character(string _name, string _gender)
{
name = _name;
gender = _gender;
}
public void SayHi()
{
//this just allows us to see if it created properly
Debug.Log("Hello my name is " + name + " and I am " + gender);
}
void On$$anonymous$$ouseDown()
{
}
}
the we want a CharacterGenerater script which will be
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public static class CharacterGenerator
{
public static List<string> genders = new List<string> { "$$anonymous$$ale", "Female", "Ambiguous" };
public static List<string> names = new List<string> { "John", "Bob", "FOO" };
public static Character $$anonymous$$akeRandomChar()
{
//we need a random value per data
// i am only using genderand name but you can add more
int rndG = Random.Range(0, genders.Count);
int rndN = Random.Range(0, names.Count);
//now we create a new char
Character newChar = new Character(names[rndN], genders[rndG]);
//return the new char
return newChar;
}
}
and finally we will make the ListSetUp which will be attached to a game object to work
using UnityEngine;
using System.Collections;
public class ListSetUp : $$anonymous$$onoBehaviour
{
Character mChar;
void Start()
{
//remeber that $$anonymous$$akeRandomChar returns a Charater object
mChar = CharacterGenerator.$$anonymous$$akeRandomChar();
mChar.SayHi();
}
}
this is how i would approach the problem but there are many other was also I hope this works out for you.
@runalotski I'm getting a lot of errors and nothing prints in the Debug Log. I'm a total newbie at coding and I have tried to use my text books and the internet to fix things, but nothing is helping. This is what I have so far. using UnityEngine; using System.Collections;
public class Random : $$anonymous$$onoBehaviour
{
Character mChar;
void Start()
{
mChar = CharacterGenerator.$$anonymous$$akeRandomChar();
mChar.Greeting();
}
}
The following is a separate script.
using UnityEngine;
using System.Collections;
public class Character
{
public string gender;
public string age;
public string style;
public string personality01;
public string personality02;
public string role;
public string race;
public string accessory;
public Character(string _gender, string _age, string _style, string
_personality01, string _personality02, string _role, string _race,
string _accessory)
{
gender = _gender;
age = _age;
style = _style;
personality01 = _personality01;
personality02 = _personality02;
role = _role;
race = _race;
accessory = _accessory;
}
public void Greeting()
{ Debug.Log("Hello my age is " + age + " and I am " + gender); }
}
Here's the code that's giving me errors. One of which is, the random.range says that there is no definition in "Random" for "range". The other is that the "Character" that comes after the keyword, "new" is telling me that it's lacking a definition.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public static class CharacterGenerator {
public static List<string> genders = new List<string> { "$$anonymous$$ale", "Female", "Ambiguous" };
public static List<string> ages = new List<string> { "Child", "Teen", "Adult", "Elder", "Ageless" };
public static Character $$anonymous$$akeRandomChar()
{
int rndG = Random.Range(0, genders.Count);
int rndN = Random.Range(0, ages.Count);
//now we create a new char
Character newChar = new Character(ages[rndN], genders[rndG]);
//return the new char
return newChar;
}
}
If you want i can go through it with you on skype if you are about, I can show you how to read a stack trace and debug issues so it will be easier to look for help in the future.
Although I noticed you called your class Random this will hide UnityEditor.Random so the class you called Random change its name to something else
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Null reference at for loop (list) 0 Answers
Do C# Lists start at 1 or 0 1 Answer