Search Unity

Question about the "on click ()" section of the "button script" area for the button (inspector)

Discussion in 'Immediate Mode GUI (IMGUI)' started by DescartesDemon, Nov 25, 2014.

  1. DescartesDemon

    DescartesDemon

    Joined:
    Dec 3, 2013
    Posts:
    15
    Hi,

    Be gentle because I have very little programming skills... and I've come from Flash in which I know minuscule amounts of AS2 (of which I use behaviors, which is a simple UI to create code without coding). I'm attempting to learn Unity so I can showcase very simplistic fake prototypes in which I demonstrate rudimentary UIs. What I mean by this is simple click this button and take me to screen X, click a button on screen X, and it takes me to screen Y.

    That being said, I was tinkering with creating a UI button and assigning it an image. So I made a button and then made it a cannon ball bomb, and then when I click it, it shows a cartoon explosion. I was able to find a parameter called "on click ()" within the button's inspector section. I assigned imageOverwrite.sprite from the drop down menu and chose the explosion. Tested, and it works.... I was then attempting to click again and have it show it's original state the cannon ball. I know very little about Even Triggers but spent a good hour attempting to find something that reset and/or create another sprite overwrite to go back to normal. None of which worked.

    So far this works:
    1. Click cannon ball.
    2. Image changes to explosion

    I'd like it to work like this:
    1. Click cannon ball.
    2. Image changes to explosion.
    3. Click explosion.
    4. Image changes to cannon ball. And then click... etc. etc.

    Usually once I find a few examples of code, I keep a cheat sheet of them so I can reuse them, since my brain isn't the best at coding. I always want to do most interactions in the program UI without coding, unless I really have to. Can anyone help? I come from an illustration and art background and I only need to know the simplest of codes. I appreciate any assistance. And, yes, I am really trying to learn code, it's very difficult for some people (being me).

    Thanks!
     
  2. DescartesDemon

    DescartesDemon

    Joined:
    Dec 3, 2013
    Posts:
    15
    Forgot to mention I'm using Unity 4.6 Beta.
     
  3. NickHaldon

    NickHaldon

    Joined:
    Mar 19, 2014
    Posts:
    128
    So I'm just learning to but I might be able to help you with this. I recently learned about a case system. I think that's what its called. That's they way I would do it for now. Don't know if there is a better, there probably is.

    Anyway, onto the code. Here is what I would do,
    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class Test : MonoBehaviour {
    7.  
    8. public bool display1;
    9. public bool display2;
    10.  
    11. public enum States{
    12. BOMB,
    13. EXPLOSION
    14. }
    15.  
    16. private States currentState;
    17.  
    18. void Start () {
    19. currentState = States.BOMB;
    20. }
    21.  
    22. void Update () {
    23. Debug.Log (currentState);
    24. switch(currentState){
    25. case (States.BOMB):
    26. display1 = true;
    27. display2 = false;
    28. break;
    29. case (States.EXPLOSION):
    30. display1 = false;
    31. display2 = true;
    32. break;
    33. }
    34. }
    35.  
    36. void OnGUI(){
    37. if(GUILayout.Button ("Next State")){
    38. if(currentState == States.BOMB){
    39. currentState = States.EXPLOSION;
    40. } else if(currentState == States.EXPLOSION){
    41. currentState = States.BOMB;
    42. }
    43. }
    44. if(display1){
    45. //make a GUI Box or something and assign the textures of the bomb to is.
    46. }
    47. if(display2){
    48. //Same thing as display1.
    49. }
    50. }
    51.  
    52. }
    53.  
    I'm pretty sure this will work. Havent tested it yet though. If you need any other help just tell me.
    Oh and if you want to click the bombs and explosions instead of a button then just assign the texture to the button and make the button in the middle of the screen. If that doesn't make sense just tell me and I'll explain it better.

    Good luck!
    NickHaldon