c# - Change Material of one game object only using Ray cast and Mouse click -
i created following code see game object click cant find how change material of object click only, changes material of objects have script.
//public public int tilegroup = 0; //indicates tile group number. //private public rigidbody myrigidbody; public renderer myrenderer; public material tiledefaultmaterial; public material tileselectedmaterial; public material tilesamegroupmaterial; void start () { myrigidbody = getcomponent<rigidbody> (); myrenderer = getcomponent<renderer> (); tiledefaultmaterial = resources.load ("tiledefault", typeof(material)) material; tileselectedmaterial = resources.load ("tileselected", typeof(material)) material; tilesamegroupmaterial = resources.load ("tilesamegroup", typeof(material)) material; } void update () { if (input.getmousebuttondown (0)) { tileclick (); } } void tileclick (){ raycasthit hitinfo = new raycasthit (); bool hit = physics.raycast (camera.main.screenpointtoray (input.mouseposition), out hitinfo); if (hit) { debug.log ("hit: " + hitinfo.transform.gameobject.name); } if (hitinfo.transform.gameobject.tag == "tile") { debug.log ("hit tile!"); myrenderer.material = tileselectedmaterial; } else { debug.log ("hit empty space!"); } }
as running
void update() { if(input.getmousebuttondown(0)) { tileclick(); } }
whenever click (no matter where), input.getmousebutton(0)
true in instances of scripts. end changing color of objects. instead can try onmousedown()
method.
void onmousedown() { tileclick(); }
Comments
Post a Comment