Ok, I’m at my whits end here trying to figure out how to make a prefab a parent. Basically I have a character select screen all set up and ready to go but it won’t let me move my characters to the next scene.
This is the script I have so far, and no luck at all.
Please help if possible, as I’ve been at this for days now and now it’s like looking at a black screen.
using UnityEngine;
using System.Collections;
public class NinjaSelect : MonoBehaviour
{
private CharacterAsset ca;
private int _charModelIndex = 0;
//private int _charOutfitIndex = 0;
private string _charModelName = "";
//private string _charOutfitName = "Base";
private GameObject _characterMesh;
public Texture Fire;
public Texture Earth;
public Texture Wind;
public Texture Water;
public Texture Ninja;
public Texture arrowRight;
public Texture arrowLeft;
//private GameObject _outfitMesh;
//public GameObject playerPrefab;
// Use this for initialization
void Start ()
{
ca = GameObject.Find ("Character Asset Manager").GetComponent<CharacterAsset> ();
InstatiateCharacterModel ();
//GameObject pc = Instantiate(playerPrefab, Vector3.zero, Quaternion.identity) as GameObject;
//pc.name = "pc";
//InstatiateCharacterOutfit();
}
// Update is called once per frame
void Update ()
{
}
void OnGUI ()
{
ChangeCharacterMesh ();
RotateCharacterModel ();
//ChangeOutfitGUI();
//GUI.Label (new Rect (Screen.width * .5f - 50, Screen.height/2+150, 120, 30), _charModelName);
if (GUI.Button (new Rect (Screen.width/2-50, 40, 100, 20), "Start"))
{
Application.LoadLevel("_MountainPass");
Debug.Log("Character Model Index: " + _charModelIndex.ToString());
}
}
private void RotateCharacterModel ()
{
if (GUI.RepeatButton (new Rect (Screen.width * .5f - 155, Screen.height / 2, 40, 40), arrowLeft))
_characterMesh.transform.Rotate (Vector3.up * Time.deltaTime * 50);
if (GUI.RepeatButton (new Rect (Screen.width * .5f + 115, Screen.height / 2, 40, 40), arrowRight))
_characterMesh.transform.Rotate (Vector3.down * Time.deltaTime * 50);
}
private void ChangeCharacterMesh ()
{
if (GUI.Button (new Rect (Screen.width * .5f - 260, Screen.height - 110, 100, 100), Ninja)) {
_charModelIndex = 0;
InstatiateCharacterModel ();
Debug.Log("Character Model Index: " + _charModelIndex.ToString());
}
if (GUI.Button (new Rect (Screen.width * .5f - 155, Screen.height - 110, 100, 100), Earth)) {
_charModelIndex = 1;
InstatiateCharacterModel ();
Debug.Log("Character Model Index: " + _charModelIndex.ToString());
}
if (GUI.Button (new Rect (Screen.width * .5f - 50, Screen.height - 110, 100, 100), Water)) {
_charModelIndex = 2;
InstatiateCharacterModel ();
Debug.Log("Character Model Index: " + _charModelIndex.ToString());
}
if (GUI.Button (new Rect (Screen.width * .5f + 55, Screen.height - 110, 100, 100), Wind)) {
_charModelIndex = 3;
InstatiateCharacterModel ();
Debug.Log("Character Model Index: " + _charModelIndex.ToString());
}
if (GUI.Button (new Rect (Screen.width * .5f + 160, Screen.height - 110, 100, 100), Fire)) {
_charModelIndex = 4;
InstatiateCharacterModel ();
Debug.Log("Character Model Index: " + _charModelIndex.ToString());
}
}
private void InstatiateCharacterModel ()
{
switch (_charModelIndex) {
case 1:
_charModelName = "0";
break;
case 2:
_charModelName = "1";
break;
case 3:
_charModelName = "2";
break;
case 4:
_charModelName = "3";
break;
case 5:
_charModelName = "4";
break;
case 6:
_charModelName = "5";
break;
default:
_charModelIndex = 0;
_charModelName = "Ninja";
break;
}
Quaternion oldRot;
if (_characterMesh == null)
oldRot = transform.rotation;
else
oldRot = transform.rotation;
if (transform.childCount > 0)
for (int cnt = 0; cnt < transform.childCount; cnt++)
Destroy (transform.GetChild (cnt).gameObject);
_characterMesh = Instantiate (ca.characterMesh [_charModelIndex], transform.position, Quaternion.identity) as GameObject;
_characterMesh.transform.parent = transform;
_characterMesh.transform.rotation = oldRot;
}
}