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 Mesh + Textures (and possibly Bone structure), which can be accomplished with GetComponent().mesh = yourMesh and renderer.material = yourMaterial.

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?

1 Answer

1

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();
		}
    
    }

Thank you so much, that helped! :)

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

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?