- Home /
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:
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)
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.
Now I found out, that in fact it is visible in the game-view, just not the way it is supposed to...
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.
Your answer
Follow this Question
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