- Home /
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!
How about two sprites each with their own game object and at the same position parented to an empty game object at that same position.
Answer by Twistorian · Sep 20, 2017 at 11:54 PM
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.
Why revive such an old answer? And you can't simply say Eric5h5 is wrong lol...
$$anonymous$$y rebuttal to your rebuttal:
Snowzurfer (OP) said:
using only one GameObject.
You Twistorian said:
I have a game object that has two GameObjects
So far I'm counting 3 GameObjects on your answer buddy and OP asked for one. Case closed.
Eric5h5 answer stands right.
I see, you are correct and I misinterpreted some part of the question.
However, my answer does accomplish what the asker wanted. It's actually essentially what Erik suggested. I edited the answer to reflect this.
I answered it because it was the first instance that Google brought when I asked how to accomplish this.
Answer by Eric5h5 · Sep 19, 2014 at 11:07 PM
You can't; just use two GameObjects. Even if you could use only one, it wouldn't have any performance benefits.
yes, i need to have less gameObjects as possible, because im using a max amount of 15 gameObjects per tile on a game just because unity don´t allow me to put more than one sprite render on a gameObject, causing lag
To reduce that lag, i need to use more than one sprite into a gameObject, but that is not allowed for unity.
Is there a way to render a sprite twice?, like:
--Go to the position 1 --print this sprite --Go to the position 2 --print this another sprite
and display both sprites on that positions at the same time?
Your answer
Follow this Question
Related Questions
Eges from other sprites around it 0 Answers
2D URP doesn't clear screen after moving object 0 Answers
How to Display Hundreds of Thousands of Sprites? 2 Answers
Animating 2D Character made up of 2 or more swapable sprites 1 Answer
Can Someone Please Tell Why Graphics Do Not Look The Same as Editor When I Build my Game 3 Answers