Search Unity

Change the source image in UI.image

Discussion in 'UGUI & TextMesh Pro' started by pKallv, Aug 30, 2014.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    In a canvas I have 2 x toggles and an image that i want to change depending on what toggle is active. Could someone nice please explain how i access the "Source Image", in the Image, property in code?

    I just can't get this to work.
     
  2. Orbcreation

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
    Dunno if I am nice or if this is helpful, but the Image should have a "sprite" property that you can change. Something like:

    Code (CSharp):
    1.  
    2. Image changeableImage;
    3. Toggle toggleButton;
    4. Sprite spriteOn;
    5. Sprite spriteOff;
    6.  
    7. function SomeFunction() {
    8.     changeableImage.sprite = toggleButton.isOn ? spriteOn : spriteOff;
    9. }
    If you want to call the function upon changing the toggle, you can set the OnValueChanged event in the Inspector.
     
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    I finally did it this way:
    Code (csharp):
    1. GameObject.Find ("ImageX").GetComponent<Image> ().sprite = p1;
     
    cry-gun likes this.