Change Model On GUI Button Click.

Hi All… In a game like “Need For Speed” You have a vehicle selection screen if you press left the car on the screen will change… I’m trying to achieve something like this with models… I’m fairly new to scripting so its some trouble… I have this so far.

#pragma strict
@script ExecuteInEditMode()

var models : GameObject[];
var spawnPOS : GameObject;



function Start () {
	
}

function Update () {
	
}

function OnGUI () {
	if (GUI.Button(Rect(20,420,50,30),"Prev")){
		Prev();
	}
	if (GUI.Button(Rect(250,420,50,30),"Next")){
		Next();
	}
}

function Next () {
	
}

function Prev () {
	
}

But i have never used var models : GameObject;
I’m not even sure if this is the right way to do it… So i came here hoping someone could explain it :slight_smile: Thanks!

You could do something like the following (warning, not tested):

#pragma strict

import System.Collections.Generic;

var models:List.<Mesh>;
var modelIndex:int;

var meshFilter:MeshFilter;

function Awake() {
    if (models == null)
        models = new List.<Mesh>();

    // Cache for faster lookup.
    meshFilter = GetComponent.<MeshFilter>();
    // Set default model.
    SelectModel(0);
}

function OnGUI() {
    if (GUI.Button(new Rect(20, 420, 50, 30), 'Prev')) {
        SelectModel(modelIndex - 1);
    }
    if (GUI.Button(new Rect(250, 420, 50, 30), 'Next')) {
        SelectModel(modelIndex + 1);
    }
}

function SelectModel(index:int) {
    modelIndex = Mathf.Clamp(index, 0, models.Count);
    meshFilter.sharedMesh = models[modelIndex];
}