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 /
This question was closed Feb 06, 2019 at 04:37 PM by jimmosio for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by jimmosio · Feb 06, 2019 at 11:35 AM · uiinstantiateprefabrender

Instantiated UI prefab in canvas does not render image

I'm trying to make a radar that marks game objects with a certain component with an "icon" UI prefab. When it comes to positioning it in the right place it seems to work, but the image component does not show up, although the game object is a child (of a child) of the canvas. I tried parenting it to the canvas directly (although it's undesireable because of the way I set up the radar), but no luck. I also tried creating the game object from scratch rather than using a prefab, still no luck.

For now I'm only testing a single icon, the player's. Radar icons are created as the gameobjects are created. This is how Unity looks like when I start the game: window

The diamond sprite is quite small relatively to the "Radar" donut so even if it rendered you might not see it, but trust me, it's not there.

This is the relevant code, it's inside Start() (i tried putting it in Awake() and it makes no difference) and it instantiates the icon and sets the required paramenters, depending on what pre-defined radar icon I want to use (the commented part would generate the icon object from scratch):

 DefaultRadarIcon radarIconRef = GlobalVars.Instance.defaultRadarIcons[(int)radarIcon];
 
 GameObject instance = Instantiate(rdricon, radarTransform); //GlobalVars.Instance.canvas.transform
 
 rectTransform = instance.GetComponent<RectTransform>();
 rect = rectTransform.rect;
 image = instance.GetComponent<Image>();
 
 /*
 GameObject radarGO = new GameObject("radar_icon (" + gameObject.name + ")", typeof(RectTransform));
 rectTransform.SetParent(radarTransform, false);
 
 rectTransform = radarGO.GetComponent<RectTransform>();
 rect = rectTransform.rect;
 image = radarGO.AddComponent<Image>();
 */
 
 rect.width = radarIconRef.sideLength;
 rect.height = rect.width;
 
 image.color = radarIconRef.color;
 
 image.sprite = radarIconRef.icon;

Another thing is that setting the rectangle's width/height doesn't seem to work, it stays to whatever was the previous value (which would be 100x100 for the "from scratch" gameobject, and 12x12 for the prefab)

Thanks in advance for any insight, if I missed something I should've told feel free to ask.

unity-2019-02-06-12-12-19.png (10.0 kB)
unity-2019-02-06-12-19-54.png (507.8 kB)
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

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by xxmariofer · Feb 06, 2019 at 11:59 AM

hello, chanfe the rectwidth and height line for this

 rectTransform.sizeDelta = new Vector2(radarIconRef.sideLength,radarIconRef.sideLength);

this will for sure scale the rect, if it doesnt is a problem with radariconref. also 12x12 is really really little, since i imgine is atleast 800 width canvas, can you share screenshot of the canvas inspector and game view?

Comment
Add comment · Show 3 · 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 jimmosio · Feb 06, 2019 at 04:20 PM 0
Share

Your code seems to work. 12x12 is just a test value, it might not be optimal but it would be at least seeable, if the image rendered.

The canvas inspector: img

The game view, at this time there is a game object with a 24x24 orange diamond image at the center of the top-left donut, which clearly isn't rendering. img2

EDIT: might as well also show the icon's inspector: img3

unity-2019-02-06-17-23-58.png (78.8 kB)
avatar image xxmariofer jimmosio · Feb 06, 2019 at 04:31 PM 0
Share

hello, you can not see the image because it has no alpha, not sure were you are setting up radarIconRef.color but his alpha is 0.

avatar image jimmosio xxmariofer · Feb 06, 2019 at 04:36 PM 0
Share

...now that's embarrasing. The default value for the Color class is probably (0,0,0,0) and I just changed the RGB values -_-

Thanks for your time I guess lmao

Follow this Question

Answers Answers and Comments

198 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 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

Layout Group not working when instantiate from prefab 1 Answer

How to change Image on button click in a prefab that is instantiated 1 Answer

UI prefab instantiation problem 1 Answer

Start() of Instantiated Prefabs Not Being Carried Out? 1 Answer

Creating Dynamic UI 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