Can you set the color based on the other object's color?

I tried various experimentation to no avail. I’d like to change the color of the car to the color of the package in this case that way I could have a bunch of different crate colors (perhaps to represent different cargo).

I think I could work around my current lack of knowledge with a bunch of tags on different sprites, but that sounds/feels messy. Is there an easier way to do this? Are there any examples/tutorials of referencing other objects further on in this course?

Edit: Thanks for trying to help, and sorry for not responding before the topic was closed. I don’t think I’m at the point where I can utilize the information provided here. I assumed there would be an easier way to do things, but I suppose not. Since I can’t respond to the closed topic, I’ll just leave this edit here.

Yeah, something along the lines of:

if (material.color == Color.red)
{
// Change the color of what you want to change, to what you want to change.
}

Hi Lux,

Welcome to our community! :slight_smile:

First of all, you have to define what “color” is. The colour of the sprite is not necessarily the colour of the material. Usually, when working with sprites, the material colour is white. And the material takes the colour from the color property of the SpriteRenderer, which is a tint and usually white.

While getting the colour of the material or SpriteRenderer is simple, you would have to write your own solution for getting the colour of a sprite. The latter is way beyond the scope of this course and requires an individual solution because you will very likely not only have to analyse the entire pixel data but also define how to distinguish between different hues.

The former is relatively simple. In this example in the API, you can find m_SpriteRenderer.color = m_NewColor;, which assigns a colour to the color property. If you want to check the colour, you could use Michael’s example and modify it: if (m_SpriteRenderer.color == Color.red) {}.

Is this what you wanted to know?


See also:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms