unity3d - Set material color with unity C# -


i working on game in unity. game far have 3 cubes wish target. have them set in array , when hit tab targets switch between them based on distance. fine have run pickle , wish have cubes target turn red visual representation. seen on youtube people used following line of code :

selectedtarget.renderer.material.color = color.red; 

however not work me. seen comment saying : function render deprecated. below should work...

selectedtarget.getcomponent<renderer>().material.color = color.red; 

this code not work me either. no errors mind you, runs fine cubes not turn red. has idea on whether or not doing right? post entire script below , code on in selectedtarget(). appreciated , thank you!

private void selecttarget(){     selectedtarget.renderer.material.color = color.red; } 

i had very same problem. had no errors. need able set color property on material, , shaders have color property. had use example self-illuminated/bumped instead of mobile/vertexlit. changing color should fine can see main color property in editor/inspector.

also make sure mesh has materials. if don't have materials, if it's blank or placeholder else won't work! i'd create texture preferably white colored , small 5x5. attach texture cube. after attach can color it!

i've done cool stuff in game color.lerp, it'll fade 1 color next! example below ping pongs white red when player hit enemy indicating damage!

transform.renderer.material.color = color.lerp(color.white, color.red, mathf.pingpong(time.time * 3 * speedlerp, 1.0)); 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -