How to change sprite?

I made a custom asset to use for this very tutorial.

car_package

Basically, instead of changing colors, I want the sprite to change to a different sprite that has the same car carrying the same package. Why not? Haha.

Anyway, I know this can be accessed via spriteRenderer.sprite and calling a Sprite object at the top of the code, but for some reason, my car keeps disappearing. :frowning:

I’m sure the answer is if statements. Anyone have any ideas?

Hi,

Those sprites look really nice, and spriteRenderer.sprite is what you need. :slight_smile:

If the car keeps disappearing, there are be at least three potential reasons:

  • The car shares the same z-position as the background sprite. This might cause the infamous z-fighting effect because the rendering order of sprites is arbitrary. Make sure the car is closer to the camera than the background.
  • The alpha channel of the colour in the SpriteRenderer component is 0.
  • null got assigned to spriteRenderer.sprite. You can easily check that by taking a look at the value of the field in the Inspector at runtime. If the field says “None”, the issue might be null.

If you cannot find the issue yourself, please share your current solution. At the moment, I do not know what you did in your project.

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

Privacy & Terms