Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Aug 12, 2016 at 08:09 AM by Hellium for the following reason:

This is not a Unity question, but a pure C#-related one, and thus, does not belong to Unity Answers.

avatar image
0
Question by PTElephant · Aug 11, 2016 at 05:36 PM · c#lists

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0
Best Answer

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])


Comment
Add comment · Show 14 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PTElephant · Aug 11, 2016 at 07:50 PM 0
Share

Thank you for responding so quickly! I'll try this tonight and let you know if it works!

avatar image PTElephant · Aug 12, 2016 at 07:54 AM 0
Share

I ran into an issue. It says that randomNumber doesn't exist in the current context.

avatar image Hellium PTElephant · Aug 12, 2016 at 08:06 AM 0
Share
   Character newChar = new Character(CharacterGenerator.genders[Random.Range(0, CharacterGenerator.genders.Length)],
 CharacterGenerator.ages [Random.Range(0, CharacterGenerator.ages .Length)])
avatar image PTElephant Hellium · Aug 12, 2016 at 08:12 AM 0
Share

I changed .length to .count and that worked, but it's telling me that the character constructor needs two arguments.

Show more comments
avatar image PTElephant · Aug 12, 2016 at 12:24 PM 0
Share

@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" };
 
 
 
avatar image Runalotski PTElephant · Aug 12, 2016 at 02:24 PM 1
Share

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.

avatar image PTElephant · Aug 12, 2016 at 04:06 PM 0
Share

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

avatar image Runalotski PTElephant · Aug 12, 2016 at 04:30 PM 0
Share

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.

avatar image Runalotski Runalotski · Aug 12, 2016 at 04:33 PM 0
Share

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

Show more comments

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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

Can we use a string instead a List<> name ? [C#] 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges