- Home /
Change color on collision and keep track?
So, I'm making a 3D platform based on the idea of color. The platforms are all white until you land on them, and the object of the game is to change every platform from white to some random color before going to the exit. Is there a script that could make platforms change to a random color on collision, as well as keep track of which platforms you've landed on so it doesn't give you points for landing on platforms you've already gotten points for.
Answer by fabian-mkv · May 10, 2016 at 05:02 PM
This is a pretty broad question, so I'll give a broad answer:
1) Create the platforms.
2) Add a Rigidbody component in the inspector so that your player can collide with it. Make sure to disable gravity (you don't want the platform falling with gravity) and enable "Is Kinematic" (you don't want it to move when a player lands on it).
3) You'll need to create a script and attach it to each platform. Let's name this script Platform. In the Platform script, next to Start() and Update(), add the function OnCollisionEnter as in the example given in the linked documentation. This function gets called whenever something collides with it (e.g.: player lands on it). Have it change color in that function using:
GetComponent<MeshRenderer>().material.color = new Color(1f, 0f, 0f, 1f);
This should be enough to get you going. Read/Watch tutorials to learn more.
I only needed the script, I know about OnCollisionEnter and such, but thank you anyways, I appreciate it!
I wasn't sure exactly what you were asking for, so I assumed that you were very new. If you'd like a random color, then create a script with the following:
void OnCollisionEnter(Collision collision) {
GetComponent<$$anonymous$$eshRenderer>().material.color = new Color(Random.value, Random.value, Random.value, 1f);
}
Is that what you're looking for?
Sorry for the delayed response, my notifications weren't working. It doesn't work, unfortunately. I attached it to the platforms as directed, but it actually changed the player's color, or at least it tried to. However, the player doesn't have a mesh to render because it's a first person game.
EDIT: Never $$anonymous$$d, it does work. It's just incredibly inconsistent.
Your answer
![](https://koobas.hobune.stream/wayback/20220612080527im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to change colors in turn? 1 Answer
Make something change color for a set amount of time 3 Answers
How do I change the color of a gameobject based on number of collisions? 1 Answer
How to change the color of the ball after hitting square? 5 Answers
How do I stop characters from standing on top of each other 1 Answer