Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Snowzurfer · Sep 19, 2014 at 10:35 PM · 2drenderingspritesmultiple

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Sep 19, 2014 at 10:36 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MacDx · Sep 21, 2017 at 12:01 AM 1
Share

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.

avatar image Twistorian · Sep 21, 2017 at 05:33 PM 1
Share

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.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image guinoalaskapp · Aug 30, 2021 at 05:03 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Render sprite on sprite ignoring alpha? 1 Answer

Eges from other sprites around it 0 Answers

2D URP doesn't clear screen after moving object 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges