- Home /
how to swap a mesh using an array
I am trying to swap an object in a scene using code. I have successfully done it with materials but some objects require the whole mesh to be changed. can someone help me please my code so far is:
public var chairs : Mesh[];
function ChangeChair(inx : int){
var meshf : Mesh = GetComponent(MeshFilter).mesh;
meshf.MeshFilter.mesh = chairs[inx];
}
function OnGUI(){
if (GUI.Button (Rect (20,100,80,20), "Chair 1")) {
ChangeChair(0);
}
if (GUI.Button (Rect (20,120,80,20), "Chair 2")) {
ChangeChair(1);
}
}
Answer by sven1994 · Aug 09, 2011 at 11:11 AM
Your variable meshf contains the mesh itself, not the MeshFilter. And in the second line you try to access the MeshFilter of the mesh, but a mesh doesn't have a MeshFilter, however a MeshFilter has a mesh. What about this:
function ChangeChair(inx : int){
var meshf : MeshFilter = GetComponent(MeshFilter);
meshf.mesh = chairs[inx];
}
meshf contains the MeshFilter and in the second line the mesh of this filter is assigned. Hope it works :D
that works prefectly thankyou. i knew i hadnt referenced the mesh properly as couldnt quite understand the scripting guide. thanks again! (sorry i cannot give u a thumbs up, will try again later, its saying i dont have permission)
Your answer
Follow this Question
Related Questions
How can I change the lookout of an object? 0 Answers
Changing sound when walking in different areas. 1 Answer
How do you change the current mesh of an object to the mesh of another object in your assets? 0 Answers
How to switch texture of several 3D objects on GUI buttons click? 1 Answer
Few problems with arrays 1 Answer