- Home /
Canvas image sprite change from script
Hi guys, i have a canvas with an image child and i want to change it for a spritesheet's child from script, loading from resources (sprites folder) and i can't find how to do that
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Sprite : MonoBehaviour
{
Image icon;
Sprite[] sprites;
Sprite newSprite;
void Start()
{
icon = transform.Find("UI_Image").GetComponent<Image>();
sprites = Resources.LoadAll<Sprite>("Sprites/spritesheet");
Debug.Log(sprites[0].name);
//newSprite = ?
}
void ChangeIcon()
{
//icon.sprite = newSprite;
}
}
this is the code so far, i get an error: IndexOutOfRangeException: Index was outside the bounds of the array. Sprite.Start () (at Assets/Scripts/Sprite.cs:16)
what i want to do is change the "UI_Image" sprite that is a child inside the canvas game object, any ideas on how to do this? some people are talking about SpriteAtlas, i tried to implement a couple of things from other questions but couldn't do it, i'm kinda new to unity, i'm still learning some basic stuff. Thanks in advance.
Comment