Replace a Prefab with another in C#
I thought this would be relatively simple but its not working... Basically, use a slider control to cycle through a bunch of different prefabs that have been assigned in the editor the the script.
public class ChangeModel : MonoBehaviour { // Start is called before the first frame update public List fullModels; public Slider frameSlider; public GameObject previewModel; void Start() {
}
// Update is called once per frame
void Update()
{
}
public void ChangeHavocFrame()
{
previewModel = fullModels[Convert.ToInt32(frameSlider.value)];
} }
No errors, but the previewModel never changes as the slider changes.
My pre-fabs have several different meshes within them, as well as different box colliders.
Any insight would be appreciated!
previewModel is just a reference so you are just changing it's reference to something else. If you would like to achieve an effect of changing actual model in the scene you need to remove the old gameobject whenever previewModel changes and create a new gameobject based on the new reference.