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.
– ChernoCouldn't you just have two models as children of a Parent object and then enable & disable them?
– LucasMarsI'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?
– Sachnmacha