- Home /
cant get characters to load
i've been trying to get a character selection screen up and running for my game and right now i have it where it will recognize which character it needs to load but when i run the game it only gives me empty game objects instead of the character and i get an error saying i cant convert an int spot on a list to gameobjects
using UnityEngine; using System.Collections.Generic; using System;
public class CharacterSelectGUI : MonoBehaviour { private CharacterSelect characterSelectScript;
// Use this for initialization
void Start()
{
characterSelectScript = GameObject.FindObjectOfType(typeof(CharacterSelect)) as CharacterSelect;
}
void OnGUI()
{
if (characterSelectScript == null)
{
return;
}
List<string> marionettes = new List<string> ()
{"Fighter", "Fisherman", "Nerd", "Body Builder", "Mobster"};
for (int i = 0; i < characterSelectScript.CharactersAvailable.Length; i++)
{
int y = i * 100;
if (GUI.Button(new Rect(100, y, 200, 75), marionettes[i]))
{
characterSelectScript.CharacterIndex = i;
Debug.Log(String.Format("Loading game play level with "+ marionettes[i]+ " selected.", i));
// Application.LoadLevel("level1");
//Instantiate (characterSelectScript.CharacterIndex[i]);
if (i > 0 && marionettes[i] != null)
{
Destroy((GameObject)characterSelectScript.CharacterIndex[i]);
}
Instantiate((GameObject)characterSelectScript.CharacterIndex[i]);
}
}
}
}
and
using UnityEngine; using System.Collections;
public class CharacterSelect : MonoBehaviour {
[HideInInspector]
public int CharacterIndex;
[HideInInspector]
public GameObject CharacterSelected;
public GameObject[] CharactersAvailable;
void Awake()
{
DontDestroyOnLoad(this.gameObject);
}
}
Answer by hbalint1 · May 04, 2015 at 08:04 PM
here
Destroy((GameObject)characterSelectScript.CharacterIndex[i]);
and here
Instantiate((GameObject)characterSelectScript.CharacterIndex[i]);
you are using the CharacterSelect class CharacterIndex variable, which is an integer. The destroy and the instantiate methods need GameObject paramters.
I think you want to do something like this:
Instantiate((GameObject)characterSelectScript.CharactersAvailable[i]);
Your CharactersAvailable variable holds an array of GameObject with the characters, so you can instantiate from the array (and the same for Destroying).