Search Unity

Change UI GUI Image Sprite at runtime .. Unity 5 .. js

Discussion in 'UGUI & TextMesh Pro' started by Griffo, Aug 25, 2015.

  1. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    Hi,

    I'm trying to swap a image sprite of a UI GUI but it's not swapping, what is wrong with my code ? .. I'm getting no errors.

    Thanks.

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. import UnityEngine.UI;
    4.  
    5. private var GUIweaponSprite : Sprite;
    6.      
    7. function Start ()
    8. {
    9.       GUIweaponSprite = GameObject.Find("Info Disk Weapon").GetComponent.<Image>().sprite;
    10. }
    11.      
    12. function OnGUI ()
    13. {
    14.        GUIweaponSprite = _weapons[2];
    15. }
     
  2. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    Answered correctly in Unity Answers ..

    Code (JavaScript):
    1.   private var GUIweaponImage : Image;
    2.      
    3.   function Start ()
    4.   {
    5.         GUIweaponImage = GameObject.Find("Info Disk Weapon").GetComponent.<Image>();
    6.   }
    7.      
    8.   function OnGUI ()
    9.   {
    10.          GUIweaponImage.sprite = _weapons[2];
    11.    }
     
  3. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    Well for one. You DON'T do it in the OnGUI update. That is just for the legacy GUI system.
    Better to do any UI changes in LateUpdate after the UI has finished drawing the current frame, ready for the next one.

    P.S. also not entirely sure why you are doing a GameObject.Find in startup, unless you have a global GO storing your images. Wouldn't you just get it from the GO the script is attached to? (no need for find, just use GetComponent)
     
  4. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    SimonDarksideJ

    Thanks for the input, I
    know I shouldn't be calling it in OnGUI update, I just grabbed some code that I've been updating from old GUI code to the new UI. The call now is in LateUpdate().

    As for doing a GameObject.Find in startup I have multiple Image components on one Canvas so by just calling GetComponent wouldn't it change them all ?

    I have the weapon image, players health image, enemy health image, armour image and bullet image, all on the same canvas ..
     
  5. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    I suppose it wouldn't, but then wouldn't it be easier to have an array property on the GameObject and pick from that. Then picking from the array of images attached to the GO instead.

    What you are doing in start is fine, however, if you do that for a lot of objects in a scene, it could impact startup performance a lot, then it'll be a lot of work to fix.

    Unity is built around the GO structure, so in most cases it's better to work with it.

    If you want to have a central place to manage the arrays, then have one empty GO with a script attached with all the image arrays, add the images to the empty GO in the editor and then access them via script (much faster)
    Either that, or have a script that loads the source sprites / images directly.

    Many ways to tackle it.
     
  6. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    Just thought of one other option, to create an asset array using a custom asset definition. Then you collate all your groups of sprites together, then simply assign the custom array asset to the GO's. I did this for a conversation system in my book but you could do the same with images if that's how you are structuring them.
    Here's one example for creating custom assets, if that's the path you want to choose
    http://www.jacobpennock.com/Blog/unity-pro-tip-use-custom-made-assets-as-configuration-files/
     
  7. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    Thanks for that information much appreciated, I'll take a look though that link, always something new to learn ..
     
    SimonDarksideJ likes this.