c# - Unity3D change MeshRender form a GameObject -
im trying replace entire mesh or gameobject ( not instantiate or disable , enable other ), because mesh different other gameobject, position , script same, so, thats why need change only mesh ( guess right way ).
so, code in c#
public gameobject mymaingameobject; public gameobject[] othermeshmaterials; int maxmaterials; int arraypos = 0; void start () { maxmaterials = othermeshmaterials.length-1; debug.log ( "total = "+ maxmaterials ); } void updatematerials() { //cycle forward if (input.getkeydown (keycode.u)) { if (arraypos == maxmaterials) arraypos = 0; else arraypos++; mymaingameobject.getcomponent<meshrenderer>().material = othermeshmaterials[arraypos].getcomponent<meshrenderer>().material; mymaingameobject.getcomponent<meshfilter>().mesh = othermeshmaterials[arraypos].getcomponent<meshfilter>().mesh; debug.log ( "name = "+ mymaingameobject.transform.name ); } //cycle backwards if (input.getkeydown (keycode.y)) { if (arraypos == 0) arraypos = maxmaterials; else arraypos--; mymaingameobject.getcomponent<meshrenderer>().material = othermeshmaterials[arraypos].getcomponent<meshrenderer>().material; mymaingameobject.getcomponent<meshfilter>().mesh = othermeshmaterials[arraypos].getcomponent<meshfilter>().mesh; debug.log ( "name = "+ mymaingameobject.transform.name ); } } void update () { updatematerials (); }
when change another, mesh smaller other (but not problem yet) , no longer changes, name changed, change of mesh happens once , never again ...
i use 4 or more othermeshmaterials test more options changed once.
thanks help!
edited script go arcamera.
here image of 2 game objects: http://www.loverde.com.br/temp_files/unity_meshstochange.png
1 - hexa_go > it_hex_3450 > pms_hexa5034 ( game object link script )
2 - eko_go > it_eko_3450 > pms_eko_5034 ( game object link script )
i trying change pms_hexa5034 pms_ekoxxx , vice/versa. change 1 , never go , never foward.
edited
please, see comments on approved answered find do.
thank @gunnar-b days
[see edit 3 solution]
well, tried code this:
i created empty gameobject , attached script it. created cube , assigned "mymaingameobject". after created sphere, capsule, cylinder , cube , 4 materials (just 4 different colors), 1 added each, 1 cube both cubes. set size of "othermeshmaterials" 4 , assigned 4 additional gameobjects (0: cube, 1: sphere, 2: capsule, 3: cylinder).
with setup cycling works fine, forward , backward. there no name change though (as not in code right now) , of course size changes objects have different size default.
also, having 4 reference gameobjects prefabs in folder , not instantiated in scene works fine.
so, far can tell, problem not related code itself. need more information stuff around script. how set up?
edit: oh well, replicated problem now. didn't quite copy script, changed capitalization @ few points: variable names starting small letter (not private ones, public ones) , methods capital letter. changed them match code again , reason killed it. reverting doesn't work more.
edit 2: ok, spotted problem: reference objects changed loosing assigned mesh , material. if select reference object , watch in inspector you'll see "type mismatch" during runtime , "missing (mesh)" not runtime.
edit 3: ok, found solution: need use "sharedmaterial/sharedmesh" instead of "material/mesh".
so adjust following (for u , y or put these 2 lines in seperate method , call it):
mymaingameobject.getcomponent<meshrenderer>().material = othermeshmaterials[arraypos].getcomponent<meshrenderer>().sharedmaterial; mymaingameobject.getcomponent<meshfilter>().mesh = othermeshmaterials[arraypos].getcomponent<meshfilter>().sharedmesh;
Comments
Post a Comment