Search Unity

Cycling through background images with button

Discussion in 'Scripting' started by Ohyouknow, May 2, 2019.

  1. Ohyouknow

    Ohyouknow

    Joined:
    Oct 23, 2013
    Posts:
    121
    Hi all,

    I've search all throughout google and can't seem to find a fix for what I believe is most likely the easiest fix ever for someone that knows coding, I however am more of an artist, than coder so any help is appreciated.

    I have a scene which has 6 backgrounds. There are 6 buttons in the UI which when pressed will change the background to the images of the character they saw on the button(s). My issue is, I have the layers sorted 1-6, but once you go past the 1st layer, the buttons will no longer bring the background back to the front. Essentially, what I have right now is buttons that work once, but I would like to be able to cycle through and anytime I press a button, it will bring it's image to the front.

    Any help?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Without code, the only help I can offer is "When you reach the end of the list, start over at the beginning again."
     
  3. Ohyouknow

    Ohyouknow

    Joined:
    Oct 23, 2013
    Posts:
    121
    @Kurt-Dekker what do you mean by that?

    the way I currently have it setup is each button opens a new scene, which works, but I don't like the split second load it does in between. I rather it be smooth. is there an easier alternative to what I'm trying to do with minimal code?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Okay, my turn to be confused. Nowhere in your original post do you mention separate scenes or split second load lags. Are we talking about the same issue?
     
  5. Ohyouknow

    Ohyouknow

    Joined:
    Oct 23, 2013
    Posts:
    121
    Yes,

    sorry - So what I meant to say is, the way I had it originally was with each button opening a separate scene. Thats not the way I want it to function, and it makes no sense to open a new scene, just for a background image to change.

    My goal is to have it function where pressing on any of the buttons will change the background image, in any order they're pressed so that it doesn't stop working once the last layer is opened.
     
  6. Ohyouknow

    Ohyouknow

    Joined:
    Oct 23, 2013
    Posts:
    121
  7. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Have a single scene with a UI canvas & an Imagr that will be the background & your buttons.

    Write a script that has two variables an Image & a Sprite.

    Add a function that is public that sets the Images sprite to the sprite variable.

    Now for each button add that script & set the sprite to the background image you would like & the Image to the UI Image. Make each button call there scripts function.

    That should do it.
     
  8. Ohyouknow

    Ohyouknow

    Joined:
    Oct 23, 2013
    Posts:
    121
    is there a simple script you could point me to or a tutorial somewhere? Again, I'm a beginner when it comes to coding as I mainly do artwork, so even something like this goes over my head easily.
     
  9. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,634
    Here is the API for UI.Image:
    https://docs.unity3d.com/ScriptReference/UI.Image.html
    You can see there is a property called sprite, so you'd just need to write a script with reference to the UI.Image, a reference to a sprite asset, and a method to set the sprite property. Make that function a listener for button press event.