Issues with Instantiate and loading sprites.
Hi guys, So I'm quite new to unity and I've come across a problem when trying to instantiate a UI image, then change its source image using Resources.Load, however, it won't instantiate the object. I am also struggling to get to grips with "Resources.Load" in javascript and change the source image of a UI image. This is my script: var item : GameObject; var inventoryCanvas : GameObject; static var itemsList : Array = ["WoodFenceT1"];
function Start () {
//Instantiates all the items on the correct slots.
createItems();
}
function Update () {
}
function createItems () {
for (var i : int = 0; i < itemsList.length; i++) {
var itemClone = Instantiate(item, inventoryCanvas.transform.position, inventoryCanvas.transform.rotation);
itemClone.transform.position = GameObject.Find(i.ToString()).transform.position;
itemClone.overrideSprite = Resources.Load("WoodFenceT1") as Sprite;
itemClone.transform.SetParent(inventoryCanvas.transform);
}
}
Any help on why nothing gets instantiated and why Resources.Load won't work will be highly appreciated. Thanks
Are you certain that nothing appears in the project view childed to the inventory canvas? it might be very small because you're not using the overloaded call to set worldPositionStays
to false. Try the following:
itemClone.transform.SetParent(inventoryCanvas.transform, false);
If that doesn't work, take two aspirin and call us in the morning.
Thanks for the suggestion, but it still wont work if I try that :(. Is there anything else I can try?