Search Unity

Changing uGUI Image sprite during runtime

Discussion in 'Immediate Mode GUI (IMGUI)' started by Levyguy, Sep 17, 2014.

  1. Levyguy

    Levyguy

    Joined:
    Jul 30, 2014
    Posts:
    1
    Hey,
    I'm trying to change a UI Image sprite using script but nothing seems to work
    this is my code

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class SLOT : MonoBehaviour {
    6.     public Image Item; // I have attached the target UI Image using the inspector
    7.  
    8.     void Start () {
    9.         Item.overrideSprite = (Sprite)Resources.Load("Sprites/100");
    10.     }
    I've also set the target image to "Sprite (2D and UI)"
    I tried using .sprite instead of .overrideSprite but it didnt work as well..
     
    Last edited: Sep 17, 2014
  2. roger-cheng

    roger-cheng

    Joined:
    Mar 15, 2013
    Posts:
    5
    what's the type of return from Resources.Load("Sprites/100")? I must be a texture, and it can not be converted to sprite
     
  3. LouisSong

    LouisSong

    Joined:
    Apr 13, 2013
    Posts:
    4
    Code (CSharp):
    1. image.sprite = (Sprite)Resources.Load("Sprites/spriteName", typeof(Sprite));
    This will be work.You should tell it which type of Source will be Load.