- Home /
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.
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!
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.