Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Pyppo · Feb 12, 2018 at 06:40 PM · instantiatetransformspritecanvasparent

Setting parent of instantiated sprite

I'm trying to instantiate indicator sprites to my UI, which for some reason doesnt work. I made prefabs of them and now I'm trying to instantiate them with the following commands:

 public Sprite indicatorR;
 
 Sprite rightIndicator;
 
 rightIndicator = Instantiate(indicatorR) as Sprite;
 
  rightIndicator.name = "indicatorImageR";
  rightIndicator.transform.parent = canvas.transform;

Sadly, Unity tells me that Sprites don't have a definition for "transform", so this for some reason doesn't work.

What do I have to do instead?

thanks C:

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by hexagonius · Feb 12, 2018 at 06:51 PM

a sprite goes onto a spriterenderer, which must be a component on a gameobject. either assign the sprite to an existing SpriteRenderer, or you make the whole gameobject a prefab (saves you the code for assignment and stuff) and instantiate that. If you're using UGUI for that, than you mixed this up with Image, which is the UGUI component. same advice from my side, but setting the parent would the need the call SetParent(parenttransform, false)

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 Pyppo · Feb 12, 2018 at 08:36 PM 0
Share

It works, but I can't manage to get it shown in game.

I created an empty GameObject and attached a Sprite Renderer to it, then made it a prefab and kept on going. The problem now is, that when I test the indicators, the Images work like they should in the Scene-view, but I can't find them anywhere in the Game-view.

Here's what it looks like, maybe that helps.alt text

alt text

unbenannt.png (252.4 kB)
unbenannt2.png (35.3 kB)
avatar image Pyppo · Feb 12, 2018 at 09:39 PM 0
Share

Now I found out, that in fact it is visible in the game-view, just not the way it is supposed to...

alt text

unbenannt.png (155.9 kB)
avatar image
0

Answer by MayIAsk · Feb 12, 2018 at 06:51 PM

Hello, Maybe you should instantiate a GameObject with an Image Component (or Sprite Component if it's not a UI element) directly.

Comment
Add comment · 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

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

138 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Getting transform from an instantiated clone to parent it? 1 Answer

Why does transform.Rotate not move the sprite and BoxCollider2D together? 1 Answer

how do i know where my instantiated object came from? 2 Answers

Parenting Instantiated objects on Collision 2 Answers

Set parent of instantiated 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