- Home /
How can I change the lookout of an object?
Hy all! How can I change the lookout of an object? I tried to change the mesh, but only the meshfilter changed, I can see the same ingame with this code:
this.gameObject.GetComponent<MeshFilter>().mesh=m.mesh;where m is a MeshFilter variable. Tried this too:
GameObject i=Instantiate(Resources.Load("spy",typeof(GameObject))) as GameObject; this.gameObject.GetComponent<MeshFilter>().mesh=i.GetComponent<MeshFilter>().mesh;this didn't achieved anything... Tried this:
Vector3 pos=this.gameObject.transform.position; Vector3 rot=this.gameObject.transform.eulerAngles; Vector3 sc=this.gameObject.transform.localScale; Destroy(this.gameObject); GameObject g=Instantiate(Resources.Load("spy"),pos,Quaternion.identity) as GameObject; g.transform.localScale=new Vector3(822.7944F,822.7944F,822.7925F);It achieved the old object has destroyed, but the new one has the same size as the original, but looks like squashed. The collider has good size too.
I don't know what should I do to change the lookout of an object... Please help me!
The code formatting is wrong: you should skip a blank line, place the tag <pre> in the next, paste your code and close it with a </pre> tag.
I edited your question, but editions are not currently updating due to some UA bug (they eventually appear after some time), thus I posted the whole question in this comment:
Hy all! How can I change the lookout of an object? I tried to change the mesh, but only the meshfilter changed, I can see the same ingame with this code:
this.gameObject.GetComponent().mesh=m.mesh;
where m is a $$anonymous$$eshFilter variable. Tried this too:
GameObject i=Instantiate(Resources.Load("spy",typeof(GameObject))) as GameObject; this.gameObject.GetComponent().mesh=i.GetComponent().mesh;
this didn't achieved anything... Tried this:
Vector3 pos=this.gameObject.transform.position;
Vector3 rot=this.gameObject.transform.eulerAngles;
Vector3 sc=this.gameObject.transform.localScale;
Destroy(this.gameObject);
GameObject g=Instantiate(Resources.Load("spy"),pos,Quaternion.identity) as GameObject;
g.transform.localScale=new Vector3(822.7944F,822.7944F,822.7925F);
It achieved the old object has destroyed, but the new one has the same size as the original, but looks like squashed. The collider has good size too. I don't know what should I do to change the lookout of an object... Please help me!
DGArtistsInc: I mean what I can see when watching that object. For example there's a person1, and I want to see that it looks like person2 if I click on him.
I have recorded the whole problem recently, you can watch that here: http://www.youtube.com/watch?v=-m$$anonymous$$j22iwTUs
Answer by aldonaletto · Mar 03, 2012 at 06:22 PM
You can replace the object's mesh this way:
(GetComponent<MeshFiter>() as MeshFilter).mesh = newMesh;
If the object has a mesh collider, you must update it too:
(GetComponent<MeshCollider>() as MeshCollider).sharedMesh = newMesh;
NOTE: Be aware that the mesh collider update is a heavy operation, and may make your game crawl like a wounded snail if done each Update!
It says: Cannot convert type UnityEngine.$$anonymous$$eshCollider' to
UnityEngine.$$anonymous$$eshFilter' via a built-in conversion
I have recorded the whole problem recently, you can watch that here: http://www.youtube.com/watch?v=-m$$anonymous$$j22iwTUs
It says: Cannot convert type UnityEngine.$$anonymous$$eshCollider' toUnityEngine.$$anonymous$$eshFilter' via a built-in conversion -> $$anonymous$$y fault! I just copy/pasted the first line and changed the GetComponent type, but forgot to change the casting type too. Now this asha$$anonymous$$g error has been fixed.
This is an animated mesh! I don't know much about animations, but suppose you can't replace the mesh so easily in this case. You could replace the material to get simple changes like different colors or add visual details like blood stains or bullet holes. To morph the character to a different shape (like transfor$$anonymous$$g Bruce Banner in the Hulk, for instance), I suppose you should edit the model in your 3D editor and add it as an animation.
Well, I'm using that script you wrote me, but I can see the same thing ingame, the mesh is the same. I think this line:
(GetComponent<$$anonymous$$eshFiter>() as $$anonymous$$eshFilter).mesh = new$$anonymous$$esh;Should change the mesh what I can see in the game, but it just replace the meshfilter component, and I can see the same object. The collider has changed because of the second line.