- Home /
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 :) Thanks!
I advise you to learn about arrays, than it should be easy.
http://docs.unity3d.com/Documentation/ScriptReference/Array.html
Answer by numberkruncher · Aug 15, 2013 at 02:38 PM
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];
}
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Assigning two function to a GUI button? 2 Answers
How to return from the last case to the first 1 Answer
Changing gravity direction... 5 Answers
Prefabs losing connection to Monodevelop scrips after switching platform 0 Answers