- Home /
Changing model during runtime after button pressed
This is my first post, if I did anything wrong please point it out!
I want to change the model of game object after a button gets pressed.
I have written the code, but I can't find the problem...
using UnityEngine; using System.Collections;
public class Player1Controller : MonoBehaviour {
public GameObject model1;
public GameObject model2;
public GameObject model3;
private GameObject currentModel;
void Start ()
{
currentModel = Instantiate (model1, transform.position, transform.rotation) as GameObject;
currentModel.transform.parent = transform;
}
void Update ()
{
if (Input.GetButtonDown ("Jump"))
{
if (currentModel == model1)
{
GameObject thisModel = Instantiate (model2, transform.position, transform.rotation) as GameObject;
Destroy (currentModel);
thisModel.transform.parent = transform;
currentModel = thisModel;
}
else
{
if (currentModel == model2)
{
GameObject thisModel = Instantiate (model3, transform.position, transform.rotation) as GameObject;
Destroy (currentModel);
thisModel.transform.parent = transform;
currentModel = thisModel;
}
else
{
GameObject thisModel = Instantiate (model1, transform.position, transform.rotation) as GameObject;
Destroy (currentModel);
thisModel.transform.parent = transform;
currentModel = thisModel;
}
}
}
}
}
thank you all for your help!
You have to be careful with the terms you use. If you want a the model to change, that would mean the $$anonymous$$esh + Textures (and possibly Bone structure), which can be accomplished with GetComponent().mesh = your$$anonymous$$esh and renderer.material = your$$anonymous$$aterial.
Couldn't you just have two models as children of a Parent object and then enable & disable them?
I'm sorry, I am really new to Unity. Basically I have 3 different 3D models with different materials. and uppon pressing a button it shall swap from 1 to 2, 2 to 3, or 3 to 1. I don't really understand what you suggest, could you explain it a little further?
Answer by b1gry4n · Oct 10, 2014 at 07:56 PM
Instantiating and destroying models over and over isnt as efficient as just turning the models on/off. For the below script to work you would need to drag/drop the models into the corresponding model slot in the inspector(the panel that shows up inside unity).
public GameObject modelA;
public GameObject modelB;
public GameObject modelC;
private int modelNumber;
void Start ()
{
modelNumber = 1;
modelB.SetActive(false);
modelC.SetActive(false);
}
void ModelSwitch(){
if(modelNumber == 1){
modelA.SetActive(false);
modelB.SetActive(true);
modelNumber = 2;
}else if(modelNumber == 2){
modelB.SetActive(false);
modelC.SetActive(true);
modelNumber = 3;
}else if(modelNumber == 3){
modelC.SetActive(false);
modelA.SetActive(true);
modelNumber = 1;
}
}
void Update ()
{
if (Input.GetButtonDown ("Jump")){
ModelSwitch();
}
}
Can you do the same thing but with an array? I can't seem to figure it out though I know it's simple :( @b1gry4n
You sure can!
public GameObject[] modelArray;
private int modelNumber;
void Start()
{
modelNumber = 0;
$$anonymous$$odelSwitch();
}
void $$anonymous$$odelSwitch()
{
for (int x = 0; x < modelArray.Length; x++)
{
if (x == modelNumber)
{
modelArray[x].SetActive(true);
}
else {
modelArray[x].SetActive(false);
}
}
modelNumber += 1;
if (modelNumber > modelArray.Length - 1)
{
modelNumber = 0;
}
}
void Update()
{
if (Input.GetButtonDown("Jump"))
{
$$anonymous$$odelSwitch();
}
}
how could this be addapted for if i wanted to press 1 for model2 and 2 for model 3 but when i change to model 2 the slot for model 3 becomes 1? example Digimon masters obline: i have gabumon i press 1 turns to gururmon but then to go to its third form from there i press one again
does that make anysense?
Your answer
Follow this Question
Related Questions
How can I change sword? 2 Answers
Gravity Direction Change 4 Answers
Bone weight set/get is very slow?? 2 Answers
Braking animation 0 Answers
Changing Camera 3 Answers