1
\$\begingroup\$

So I have a bunch of cubes instantiated. Each time the player hits one of these cubes (which is quite often) they change their color to match that of the player. What would be a performant way to do this? So far i am doing this on OnTriggerEnter then grab my player’s mesh renderer, get its material and get the color. This is quite an expensive process. I was wondering if there is any better way. The cubes do not move by the way, so if they can somehow be batched as well that would be a plus! Also, the player’s color is random.

\$\endgroup\$
0
\$\begingroup\$

I would add a static refrense to the color in the player, something like: public static Color publicColor (keep in mind to update this color as well) and when any cubes want to chage color they just say meshrenderer.material.color = myPlayer.publicColor If you want a performance boost, but also keep in mind that this will only work with 1 player.

\$\endgroup\$
  • \$\begingroup\$ Ah there more than one player \$\endgroup\$ – Joe Robert Aug 26 at 6:22
  • \$\begingroup\$ Then I whould have some sort of array/list like public static Color[] publicColor where cube1 uses the first slot, c2 the second. And when a cube want to change color they just meshrenderer.material.color = myPlayer.publicColor[CUBEINDEX] (myPlayer is the class not the gameobject script) \$\endgroup\$ – Vincent Lagerros Aug 26 at 6:28
  • \$\begingroup\$ (Also spawn pooling is a good way to keep preformese high) \$\endgroup\$ – Vincent Lagerros Aug 26 at 6:29
  • \$\begingroup\$ I would say each player will be having their own color and when player collider with cube then cube just need to grab player color so you don't need to have array or list of color. So by this you don't need to get mesh render and material but the color of the player will be present on the go. \$\endgroup\$ – Swati Aug 26 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.