Unity 2D C# Instantiate sprite on canvas. Can't find what's wrong

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Unity 2D C# Instantiate sprite on canvas. Can't find what's wrong



I read many questions about this, but I still can't find what my problem is...
I'm trying to instantiate a prefab at the canvas. It is compose of a button and a sprite. The button looks ok, but the sprite is not visible at the Game (but is visible at the Scene).



I'm doing something wrong, but I can't see what...


[SerializeField] GameObject finishedLevel;

private void Start()

finishedLevel = Instantiate(finishedLevel, transform.position, transform.rotation);
finishedLevel.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform, false);




enter image description here




1 Answer
1



SpriteRenderer is not made to be used with the Canvas. You are confusing and misusing the two.



SpriteRenderer is used for displaying 2D Objects like a 2D animated character or a 2D environment. You can attach Rigidbody2D and Colliders to SpriteRenderer.


SpriteRenderer


Rigidbody2D


Colliders


SpriteRenderer



Canvas is used for UI display only. It is used for displaying things such as UI texts, buttons, sliders, scrollbars and images. You shouldn't attach Rigidbody2D and Colliders to it or its child objects.



With the explanation above, you should be able to determine which one to use. If you just need to display image under a Canvas, use the Image, or RawImage component since they are part of the UI system. This should work but do not make SpriteRenderer a child of a Canvas. If you have to use SpriteRenderer, make it its own object or under another object but it should not be under a Canvas. You may find Unity's UI tutorial useful.


Image


RawImage


SpriteRenderer


SpriteRenderer





At what point are unity going to realise they should hire you to write their manuals?
– Confused
Aug 7 at 4:42





@Confused Lol thanks
– Programmer
Aug 7 at 5:05






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard