- Home /
Object collision change colour
HI, im very new to unity and i was wondering if i can change the colour of the object that my player is touching. So like i have a platform game and i would like each object the player walks over to change colour.
Any help would be great code would be even better
Answer by Ashkan_gc · Mar 17, 2011 at 11:20 AM
ok. it's easy to do in unity like many other things. soon you will learn much man! there are two events that you can use. depending on the type of your collider you should use OnTriggerEnter or OnCollisionEnter events. if your platform is a trigger then either the platform or the player should have rigidbody attached (the rigidbody could be kinematic).
if it's a trigger you could attach this code to your platform.
void OnTriggerEnter (Collider info)
{
if(info.tag == "Player") renderer.material.color = myColor;
}
this is a small C# example code that changes the color to myColor. you could declare it as a public var to be able to change the color in inspector. also you should set the player object's tag to "Player".
there are other ways that you can see who is the object collided with you. if it's a OnCollisionEnter event then the Collision object will be sent to your event and you should get it's .gameObject.tag to check it's tag.
most rendering and color/texture properties are in renderer.material.
if i use OnTriggerEnter my player falls though the object how can i stop this?
you should either use a character controller or use rigidbodies to do it automatically or just code it yourself which i think you can not do at this time. in js just use function OnTriggerEnter (info : Collider) { //code }
im sorry it still does not work i put a rigid body on my platform and turned it kinematic and is trigger then i attached the script above to my playform and my Player already had a tag of Player my player still falls though. im using the 2D platformController
code on the platform.
function OnTriggerEnter (info : Collider) { renderer.material.color = Color.blue;
}
what you wrote don't require the player tag because it does not have any if statements to check it. does your player have any colliders (trigger/not trigger) attached? if you don't want to fall then you should set isTrigger to off and use OnCollisionEnter ins$$anonymous$$d.
Answer by smasgames · Jul 05, 2011 at 03:38 PM
how would u change the object to another object when collided with
Please don't use the answers section to ask another question.