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 Deivore · Aug 21, 2015 at 03:47 AM · 2dprefabinvisible

Invisible prefabs instantiated by Resources.Load

I want to use each image in a sliced tilesheet as a background for a dynamically instantiated toggle button. In order to do this I placed a Unity2D Toggle prefab into my resources folder. After instantiating I change its parent, location, and background image. However, the prefabs are invisible when instantiated. They are added to the hierarchy and can be selected, but are displayed neither in the scene view nor the game view.

invisible instantiation

When the prefab is dragged onto the Hierarchy from the resources folder, it is displayed without any problems. The problem I think must be somewhere in my instantiation code, in LoadTileset(int _level), possible where I call GameObject.Instantiate()

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEditor;
 using System.Collections;
 using System.Linq;
 
 public class SpriteSelector : MonoBehaviour {
 
     ToggleGroup group_sprites;
     Toggle[][] toggles;
     public Texture2D[] tilesheets;
     public Texture2D checkmark;
     int sliceSize = 32;
     int level; 
 
     public void Setup()
     {
         group_sprites = GameObject.Find ("group_sprites").GetComponent<ToggleGroup> ();
         //Slice the current thingy
         LoadTileset (0);
 
         //Make a toggle group
     }
 
     public void LoadTileset(int _level)
     {
         level = _level;
         //Slice em up
         string spriteSheet = AssetDatabase.GetAssetPath(tilesheets[level]);
         Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath (spriteSheet)
             .OfType<Sprite> ().ToArray ();
 
         RectTransform parentRect = gameObject.GetComponent<RectTransform> ();
         int buffer = 1;
         float toggleWidth = parentRect.rect.width/8 - buffer;
         float xPos = -76.45f;
         float yPos = 42.3f;
         foreach(Sprite sprite in sprites)
         {
             GameObject terrainToggle = GameObject.Instantiate(Resources.Load<GameObject> ("Toggle"));
             terrainToggle.transform.SetParent(transform);
             GameObject Background = terrainToggle.transform.FindChild("Background").gameObject;
             GameObject CheckMark = Background.transform.FindChild("Checkmark").gameObject;
 
             //location
             RectTransform trans = terrainToggle.GetComponent<RectTransform>();
             trans.sizeDelta = new Vector2(toggleWidth, toggleWidth);
             trans.anchoredPosition = new Vector2(xPos ,yPos);//+ trans.sizeDelta.x/2, yPos - trans.sizeDelta.y/2);
 
             //background
             //Background.GetComponent<Image>().overrideSprite = sprite;
             Image backImage  = Background.GetComponent<Image>();
             backImage.sprite = sprite;
             //checkmark
             CheckMark.GetComponent<Image>().sprite = Sprite.Create (checkmark, new Rect(0,0,checkmark.width, checkmark.height), new Vector2(0.5f,0.5f));
             
 
             xPos += parentRect.rect.width/8;
             if(xPos > parentRect.rect.width/2)
             {
                 xPos = -76.45f;
                 yPos -= parentRect.rect.width/8;
             }
         }
 
         //foreach slice, make a toggle
     }
 
     // Update is called once per frame
     void Update () {
 
         //if click on a tile,
     }
 }
 

Any help is appreciated!

instantiate.png (168.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

· Add your reply
  • Sort: 
avatar image
0

Answer by Deivore · Aug 22, 2015 at 03:09 AM

For anyone interested, my problem was that the prefabs were instantiated as part of an Awake() call, which evidently makes you prefabs invisible. Moving the instantiation to a Start() call fixed my problem.

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

2 People are following this question.

avatar image avatar image

Related Questions

Instantied 2D Prefab Is Invisible 1 Answer

Why is my instantiated prefab invisible ? 2 Answers

Instantiated Prefab doesn't appear? 3 Answers

Reusing tilemap segments 0 Answers

[Tilemap] Can you spawn a Prefab (with colliter) together with a tile when placing it? 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