- Home /
How to add my character to characterlist?
Here's my code: using UnityEngine; using System.Collections;
public class Characterlist : MonoBehaviour {
public bool createcharacter;
public float scrollbar;
public bool choosecast;
public float scrollbartwo;
public string name = "";
public bool showcharacter;
// Use this for initialization
void Start () {
createcharacter = false;
choosecast = false;
showcharacter = false;
}
// Update is called once per frame
void Update () {
}
void OnGUI ()
{
GUI.Box (new Rect (400, 10, 800, 800), "Character");
if (GUI.Button (new Rect (500, 100, 600, 100), "Create new character"))
createcharacter = true;
if (createcharacter) {
scrollbar = GUI.HorizontalScrollbar (new Rect (30, 40, 100, 20), scrollbar, 1.0f, 5.0f, 1.0f);
GUI.Label (new Rect (10, 10, 100, 40), "Class:");
scrollbartwo = GUI.HorizontalScrollbar (new Rect (30, 90, 100, 20), scrollbartwo, 1.0f, 5.0f, 1.0f);
GUI.Label (new Rect (10, 60, 100, 40), "Race:");
GUI.Box (new Rect (0, 0, 200, 250), "");
if (GUI.Button (new Rect (20, 200 ,150 , 40), "Create character"))
{
createcharacter = false;
showcharacter = true;
if (showcharacter == true)
{
GUI.Box (new Rect (100, 100, 100, 100), "asd");
}
}
}
if (createcharacter) {
if (scrollbar == 4) {
GUI.Label (new Rect (70, 10, 100, 40), "Mage");
}
if (scrollbar == 1) {
GUI.Label (new Rect (70, 10, 100, 40), "Warrior");
}
if (scrollbar < 4 && scrollbar > 1) {
GUI.Label (new Rect (70, 10, 100, 40), "Ranger");
}
if (scrollbartwo == 4) {
GUI.Label (new Rect (70, 60, 100, 40), "Human");
}
if (scrollbartwo == 1) {
GUI.Label (new Rect (70, 60, 100, 40), "Orc");
}
if (scrollbartwo < 4 && scrollbartwo > 1) {
GUI.Label (new Rect (70, 60, 100, 40), "Elf");
}
GUI.Label (new Rect (10, 150, 100, 40), "Name:");
name = GUI.TextField (new Rect (70, 150, 50, 20), name);
}
}
}
Answer by esitoinatteso · Oct 21, 2014 at 09:17 PM
Pardon me, but I think you're doing something wrong... I mean with a wrong logic.
If you want to add a GameObject named " character" to a list, you have to first set up a list. This class of yours called Characterlist would be a good spot to do that.
Write this as your 3rd line ( it doesn't have to be the 3rd line, but it must be before the line in which is wrote: public class Characterlist : MonoBehaviour)
using System.Collections.Generic;//This enables the Lists
Then add this in your class constructor:
public List<GameObject> ListOfCharacters = new List<GameObject>();
This way you'll have a public list set up. In the editor you can drag and drop there your characters.
You could even do so by code.
This assumes you know what a Class, a GameObject and a List are. If you don't then I encourage you to have a look at these links: