Render 2 sprites using one GameObject Unity2D

Hey everyone,

I need to render 2 sprites at the same position, one on top of the other one, using only one GameObject. How can I achieve that?

I know that I cannot manually render a sprite in Unity2D (if there is a way, please tell me), but I want to do something like (in pseudo code):

//...enter the drawing section (pseudoCode)
//...
    spriterenderer.draw(spriteOne, x, y, myAlphaValue);
    spriterenderer.draw(spriteTwo, x, y, myAlphaValue);
//...

This way I could change the alpha value of each sprite individually, so that the one on the top (second one to be rendered) would show part of the first depending on its alpha value, retaining the performance on having just one GameObject.

Thanks in advance!

You can’t; just use two GameObjects. Even if you could use only one, it wouldn’t have any performance benefits.

I happened to have the same question and I found an answer elsewhere.

Sorry for answering an old topic, but there is a workaround. This is basically also what Erik suggested. Here I lay the details of how to accomplish this.

You can have two SpriteRenderers (or whatever objects of the same type) as children GameObjects of some main GameObject. Then it’s possible to individually control them.

As such, you do need to have more than one GameObject but in the spirit of the question, this is the way to do it.

I don’t know how efficient it is, but it is certainly possible.

In this snippet of mine, I have a game object that has two GameObjects with SpriteRenderer components as children. One of the children is called “Flooding” and other is called “Floor”. To access a particular SpriteRenderer, you find the name of the object.

SpriteRenderer renderer = 
transform.Find("Flooding").gameObject.GetComponent<SpriteRenderer>();

Then to change the alpha:

Color temp_color = renderer.color;
temp_color.a = alpha;
renderer.color = temp_color;

And I confirm that it works.

I made the SpriteRenderer into a private variable of the script that controls the alpha, instanced at its Start(), so it doesn’t need to be fetched from the object every time it’s used (sounds to me more efficient than Finding it again every time it’s needed).

Here’s the documentation.