- Home /
Question by
ani2020.bhandari · Oct 19, 2014 at 03:05 AM ·
gameobjectinstantiatecharacternext
Replacing A GameObject
I've been trying to make a changeable character scene where on clicking 'next' button the character changes I am able to generate the prefab but unfortunately the earlier prefab is never destroyed. What am I doing Wrong, Thanks for the help in advance.
using UnityEngine;
using System.Collections;
public class character_Set : MonoBehaviour {
public GameObject character1;
public GameObject character2;
public GameObject cCharacter;
public int characternum = 1;
private bool startup = true;
// Use this for initialization
void Start() {
cCharacter = Instantiate (character1) as GameObject;
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
if (GUI.Button (new Rect (50, 50, 70, 40), "Next")) {
Destroy(cCharacter);
ChangeCharacters();
}
}
void ChangeCharacters(){
if (characternum == 1) {
cCharacter = Instantiate(character2) as GameObject;
characternum = 2;
}
if (characternum == 2) {
cCharacter = Instantiate(character1) as GameObject;
characternum =1;
}
}
}
Comment
Best Answer
Answer by $$anonymous$$ · Oct 19, 2014 at 10:58 AM
Just add one keyword to 8th line: "else":
private void ChangeCharacters()
{
if (characternum == 1)
{
cCharacter = Instantiate(character2) as GameObject;
characternum = 2;
}
else if (characternum == 2)
{
cCharacter = Instantiate(character1) as GameObject;
characternum = 1;
}
}
Damn That Was Dumb of $$anonymous$$e, Thanks for the Help ZyTrOn
Your answer
Follow this Question
Related Questions
Destroying all enemy characters after the level is over 4 Answers
Instantiated Objects not being set at ground/terrain level?(Solved) 1 Answer
Unwanted 2nd empty gameobject 1 Answer
Every NetworkViewID is 0? 0 Answers
NavMesh giving jerky like motion 0 Answers