Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

UI.Image change sprite on script

Discussion in 'UGUI & TextMesh Pro' started by Eugen-Pavlenko, Aug 23, 2014.

  1. Eugen-Pavlenko

    Eugen-Pavlenko

    Joined:
    Aug 23, 2014
    Posts:
    3
    I use next code
    Code (CSharp):
    1. Obj.transform.GetChild(0).GetComponent<Image>.overrideSprite =  (Sprite)Resources.Load("Textures/sprite");
    But receive error

    Type IPointerClickHandler expected Button received. i:0. Cannot cast from source type to destination type.
    UnityEngine.EventSystems.EventSystem:Update()
     
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,181
    twobob likes this.
  3. Eugen-Pavlenko

    Eugen-Pavlenko

    Joined:
    Aug 23, 2014
    Posts:
    3
    I try load sprite before change.
    Code (CSharp):
    1. Sprite sprite = (Sprite)Resources.Load("Textures/musicoff_icon");
    And received same error. But in inspector Textures/musicoff_icon is a sprite.
     
  4. Eugen-Pavlenko

    Eugen-Pavlenko

    Joined:
    Aug 23, 2014
    Posts:
    3
    Thx all.
    Correct code:
    Code (CSharp):
    1. Obj.transform.GetChild(0).GetComponent<Image>.overrideSprite =  Resources.Load<Sprite>("Textures/sprite");
     
  5. remer88

    remer88

    Joined:
    Aug 24, 2014
    Posts:
    1
    I have tryed to change the texture of an Image component using the property `overrideScript` without luck, the image shown is always the same. I need to change the image shown using a texture downloaded using the class WWW.

    My code is similar to:

    Code (CSharp):
    1.  
    2. public SomeClass : MonoBehaviour
    3. {
    4.    private void SomeMethod() {
    5.       // After image was downloaded using an instance of WWW
    6.       this.MyCustomGameObject.GetComponent<Image>().overrideSprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height, new Vector2(0.5f, 0.5f));
    7.    }
    8. }
    9.  
    Debugging I could verify that the image was correctly downloaded and that previous lines was executed.

    Also I've tryed to assign a new sprite to image's sprite property but still not working

    Any idea?
    Thanks
     
    Last edited: Aug 31, 2014
    samana1407 and DiegoBittencourt like this.
  6. s0351060

    s0351060

    Joined:
    Sep 23, 2014
    Posts:
    1
    It seems that the correct one is GetComponent<Image>().sprite not overrideSprite
     
  7. SeasiaInfotechind

    SeasiaInfotechind

    Joined:
    Nov 17, 2014
    Posts:
    32
    Hello Eugen,
    You can implement your code like this.
    public Sprite newSprtieImage;

    GetComponent<Image>().sprite = newSprtieImage;

    Thanks
     
  8. AlfonsoC

    AlfonsoC

    Joined:
    Oct 1, 2014
    Posts:
    27
    what is the difference between overrideSprite and sprite? i ask because if i set an image sprite with overrideSprite, if the sprite to set is 9sliced the image doesn't render properly

    Thanks :)
     
  9. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Override sprite is for it you want to quickly set a image without changing the original. If you want to change something on a permanent basis use sprite.
     
  10. AlfonsoC

    AlfonsoC

    Joined:
    Oct 1, 2014
    Posts:
    27
    and is normal then that a 9sliced sprite doesnt render properly with the overrideSpirte?