Search Unity

Easy enough question

Discussion in 'UGUI & TextMesh Pro' started by Fressno, Sep 6, 2016.

  1. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    As the title says, this will probably be an easy answer from you guys.
    Im making a UI for a school project.
    Its only a backgorund stretched out as an image.
    A button and another image..
    The button should enable the image that is disabled. and with another click on that button ( or another button) the image should disapear, showing a new image with another button, or just disapear completly.
    Its for a "doctors log entries" on hes computer.
    there should be buttons saying "medical log 22098"
    if that button is pressed, an image should appear showing that image, as in this case, a "log" from the "Doctor".
    there should be more buttons being similiar. medical log, showing another log entry, and collapses when you click outside the image, or press another button(log entry).
    ive managed to make the image appear, but not disapear when i press again.
    should i involve scripts?
    or the OnClick parameters down below?
    im thankfull for all the help.
    /Fressno
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    You will need scripts if you want the one button to turn it on and off.

    attach this script to the Image you want to turn and and off:
    Code (CSharp):
    1. public class ToggleImage
    2. {
    3.         public void ToggleButton()
    4.         {
    5.               // this just flips the image on and off;
    6.               this.enabled = !this.enabled;
    7.          }
    8. }
    Then set your button's OnClick to that function:
    1. Click the + Under the OnClick to add a new OnClick Listener
    2. Drag the Image down onto the None(Object) section
    3. Then the Function bar should become visible, click on it and navigate to the ToggleImage Function
     
  3. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    im getting alot of errors with this code.
     
  4. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ToggleImage : MonoBehaviour {
    5.  
    6.     public class ToggleImage
    7.     {
    8.         public void ToggleButton()
    9.         {
    10.             // this just flips the image on and off;
    11.             this.enabled = !this.enabled;
    12.         }
    13.     }
    14. }
     
  5. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Well you have two levels of ToggleImage in here.

    When you create the ToggleImage c# script it automatically makes the public class ToggleImage line
    You need to get rid of the inner ToggleImage and its surrounding braces
     
  6. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    takatok, is there anyway you could take my code above and show what you mean?
     
  7. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    is this what you mean?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor
    4.  
    5. public class ToggleImage : MonoBehaviour
    6. {
    7.    
    8.         public void ToggleButton ()
    9.         {
    10.             // this just flips the image on and off;
    11.             this.enabled = !this.enabled;
    12.         }
    13. }
    cus this doesnt work.
     
  8. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    what am i doing wrong?
     
  9. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    i found out that i forgot a ; after using UnityEditor.
    so i aplyed it to the image, used the funtion to point at the script. but nothing happends when i push the button.
    no image appears.
    ive hidden the image by boxing off it at the inspector.
     
  10. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    this is so simple, but yet it doesnt work. please guys, help me. my school project is on a stand still, and my classmates are depending on me. im in desperate need of your help.
     
  11. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    is this that hard of a thing to do?
    just to toggle an image on and off with a UI button?
     
  12. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Sorry it was very late when I wrote that script and it won't work. That function is just turning the ToggleImage script on and off not the actual Image

    If you change it to this:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEditor
    5.  
    6. public class ToggleImage : MonoBehaviour
    7. {
    8.     Image image;
    9.     void Start()
    10.     {
    11.             image = GetComponent<Image>();
    12.     }
    13.     public void ToggleButton()
    14.     {
    15.         // this just flips the image on and off;
    16.        
    17.         image.enabled = !image.enabled;
    18.     }
    19. }
    Note: this assumes your attaching this to an Image. If you attach to to something else like a UI Text. then you need to change Image image to Text text -- and all the other references to Image to Text
     
  13. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    Should i make an UI image and attach the sprite to it?
    Or should i just add a sprite directly, and add the script?
    If its a UI image (rawimage/image) , should it be named differently?
    Thanx for all the help.
     
  14. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Depends on what you want to toggle on and off. Are you trying to change a 2D Sprite on and off, a UI Image, a UI Text?

    if its a 2D Sprite you created drag the script on the sprite and Make the Button OnClick point to script. But it would have to be changed to this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor
    4. public class ToggleImage : MonoBehaviour
    5. {
    6.     SpriteRenderer spriteRenderer;
    7.     void Start()
    8.     {
    9.             spriteRenderer = GetComponent<SpriteRenderer>();
    10.     }
    11.     public void ToggleButton()
    12.     {
    13.         // this just flips the image on and off;
    14.      
    15.          spriteRenderer.enabled = !spriteRenderer.enabled;
    16.     }
    17. }
     
  15. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    thank you tatatok.
    its working!now i have another problem.
    I want to make a very easy and basic login scene for this medical log thing.
    like a password field, where you need to type the right password to load the next scene.
    ive come this far:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Password : MonoBehaviour {
    5.     public string passwordToEdit = "Cinderella";
    6.     void OnGUI()
    7.     {
    8.         passwordToEdit = GUI.PasswordField(new Rect(10, 10, 200, 20), passwordToEdit, "*"[0], 25);
    9.     }
    10. }
    i would like to know how to finnish it?
    like an if statement.
    if passwordToEdit == "Cinderella";
    Application.LoadLevel ("medicallog1")

    or something like that.
    but i cant seem to be able to get it to work.
    please help!?
    best regards, Fressno
     
  16. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Just to note: The GUI/OnGUI system is deprecated. It was replaced in Unity 4.6 by a new UI system. Though its still kept in for Legacy code. If you wanted to do this with new system you should play around with UI Text, UI Button and UI InputField. But here is how to get your current code going:

    Code (CSharp):
    1. public class Password : MonoBehaviour {
    2.     bool showInvalid = false;
    3.     // You don't have to have anything in this
    4.     // its just a placeholder to store the password they enter
    5.     public string passwordToEdit = "";
    6.     void OnGUI()
    7.     {
    8.         passwordToEdit = GUI.PasswordField(new Rect(10, 10, 200, 20), passwordToEdit, "*"[0], 25);
    9.     }
    10.  
    11.     // This makes a button and calls the code inside when they press it
    12.             if (GUI.Button(new Rect(30,30,200,20), "Submit"))
    13.         {
    14.             if (passwordToEdit == "Password")
    15.                 // Load your Scene here
    16.             else
    17.             {
    18.                 // this is just code to turn on Invalid Password, then turn it off in 5 seconds
    19.                 // This line turns the Invalid Label on
    20.                 ToggleLabel();
    21.                 //This says turn it off in 5 seconds
    22.                 Invoke("ToggleLabel",5f)
    23.             }
    24.         }
    25.  
    26.         if (showInvalid)
    27.         {
    28.             GUI.Label(new Rect(60, 60, 200, 20), "Invalid Password");
    29.         }
    30. }
    31.  
    32. void ToggleLabel()
    33. {
    34.            showInvalid = !showInvalid;
    35. }
     
  17. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    Where whould i enter the loadlevel code?
    Like if you type in the right password in a field, you trigger an if statement :
    If you type cinderella, you activate application. Loadlevel.
    Is that better explained?
    This is just an UI for a computer thats in a real life room escape game in my school.
     
  18. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    Sorry, i saw your "load your scene here" answer. Thank you again :)
     
  19. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    im getting all types of errors. check it out =)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor;
    4.  
    5. public class Password : MonoBehaviour {
    6.     bool showInvalid = false;
    7.     // You don't have to have anything in this
    8.     // its just a placeholder to store the password they enter
    9.     public string passwordToEdit = "";
    10.     void OnGUI()
    11.     {
    12.         passwordToEdit = GUI.PasswordField(new Rect(10, 10, 200, 20), passwordToEdit, "*"[0], 25);
    13.     }
    14.  
    15.     // This makes a button and calls the code inside when they press it
    16.     if (GUI.Button(new Rect(30,30,200,20), "Submit"))
    17.     {
    18.         if (passwordToEdit == "Password")
    19.             // Load your Scene here
    20.         else
    21.         {
    22.             // this is just code to turn on Invalid Password, then turn it off in 5 seconds
    23.             // This line turns the Invalid Label on
    24.             ToggleLabel();
    25.             //This says turn it off in 5 seconds
    26.             Invoke("ToggleLabel",5f)
    27.         }
    28.     }
    29.  
    30.     if (showInvalid)
    31.     {
    32.         GUI.Label(new Rect(60, 60, 200, 20), "Invalid Password");
    33.     }
    34. }
    35.  
    36. void ToggleLabel()
    37. {
    38.     showInvalid = !showInvalid;
    39. }

    errors:

    Assets/Scripts/Password.cs(16,23): error CS1519: Unexpected symbol `(' in class, struct, or interface member declaration

    Assets/Scripts/Password.cs(16,34): error CS1519: Unexpected symbol `30' in class, struct, or interface member declaration

    Assets/Scripts/Password.cs(18,37): error CS1519: Unexpected symbol `==' in class, struct, or interface member declaration

    Assets/Scripts/Password.cs(24,25): error CS1520: Class, struct, or interface method must have a return type

    Assets/Scripts/Password.cs(26,44): error CS1519: Unexpected symbol `ToggleLabel' in class, struct, or interface member declaration

    Assets/Scripts/Password.cs(28,9): error CS8025: Parsing error

    a symbol out of place maybe?
     
  20. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Yes a silly mistake by me:
    Need to take out a } brace:
    Code (CSharp):
    1. void OnGUI()
    2.     {
    3.         passwordToEdit = GUI.PasswordField(new Rect(10, 10, 200, 20), passwordToEdit, "*"[0], 25);
    4.     } <--------- Take this out
    And add it down here
    Code (CSharp):
    1.  if (showInvalid)
    2.     {
    3.         GUI.Label(new Rect(60, 60, 200, 20), "Invalid Password");
    4.     }
    5.   } <--- We added an extra } here
    6. }
     
  21. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor;
    4.  
    5. public class Password : MonoBehaviour {
    6.     bool showInvalid = false;
    7.     // You don't have to have anything in this
    8.     // its just a placeholder to store the password they enter
    9.     public string passwordToEdit = "";
    10.     void OnGUI()
    11.     {
    12.         passwordToEdit = GUI.PasswordField(new Rect(10, 10, 200, 20), passwordToEdit, "*"[0], 25);
    13.    
    14.  
    15.     // This makes a button and calls the code inside when they press it
    16.     if (GUI.Button(new Rect(30,30,200,20), "Submit"))
    17.     {
    18.         if (passwordToEdit == "Password")
    19.             // Load your Scene here
    20.         else
    21.         {
    22.             // this is just code to turn on Invalid Password, then turn it off in 5 seconds
    23.             // This line turns the Invalid Label on
    24.             ToggleLabel();
    25.             //This says turn it off in 5 seconds
    26.             Invoke("ToggleLabel",5f)
    27.         }
    28.     }
    29.  
    30.     if (showInvalid)
    31.     {
    32.         GUI.Label(new Rect(60, 60, 200, 20), "Invalid Password");
    33.         }
    34.     }
    35. }
    36.  
    37. void ToggleLabel()
    38. {
    39.     showInvalid = !showInvalid;
    40. }
    like this?
    getting more errors like this.
     
  22. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    Assets/Scripts/password.cs(20,20): error CS1525: Unexpected symbol `else'

    Assets/Scripts/password.cs(27,17): error CS1525: Unexpected symbol `}'

    Assets/Scripts/password.cs(37,6): error CS0116: A namespace can only contain types and namespace declarations
     
  23. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    should i add all of this code to the LoadLevel:
    Code (CSharp):
    1.  public void LoadScene(int level)
    2.     {
    3.         loadingImage.SetActive(true);
    4.         Application.LoadLevel(level);
    5.     }
    or just just

    Code (CSharp):
    1. Application.LoadLevel(level);
    and should i make a reference to it at the beginning?
     
  24. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Code (CSharp):
    1. // Load your Scene here
    Has to have actual load level code inserted it will complain about the else

    Its hard to tell with curly braces in this format. Looks like I added one time many in my last response:
    Code (CSharp):
    1. GUI.Label(new Rect(60, 60, 200, 20), "Invalid Password");
    2.         } <--- Take this out
     
  25. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    I would use this code as Application.LoadLevel is deprecated:

    At the top of your file add:
    Code (CSharp):
    1. using UnityEngine.SceneManagement;
    Then change
    Code (CSharp):
    1.  if (passwordToEdit == "Password")
    2.             // Load your Scene here
    to
    Code (CSharp):
    1.  if (passwordToEdit == "Password")
    2.             SceneManager.LoadScene(level);
    Note: level is some int that you have assigned the number of the Scene in your buildIndex.
    You can also just use:
    Code (CSharp):
    1. SceneManager.LoadScene(1);
    if you know the Scene you want to load is scene 1. The Buildindex is 0 based. You first scene = 0, second scene = 1 and so on.
     
  26. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    if you take that one away, you are missing one that connects to the first curly bracket.
    doesnt one need to connect at the very end to the absolute first bracket?
     
  27. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    yes. There should also be one more } at the end of the file after ToggleLabel. Toggle Label is a part of Password
     
  28. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    great, thanx =)
     
  29. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    the scene name, it cant be named with a space in between right?
    like: medical log. it should be medical_log right?
    should it be like ("medical_log") or (medical_log)?
     
  30. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    sorry, didnt read your answer that well.
    i should put in loadscene(1) in that place, cus the there is only two scenes. the password scene, and the medical log scene, thats the second scene (scene 1)
    so simple things can be so complicated.
    *sigh*
    thanx for all the help anyway.
     
  31. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    im getting more errors. this time i cant find any brackets thats wrong:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class Password : MonoBehaviour {
    7.     bool showInvalid = false;
    8.     // You don't have to have anything in this
    9.     // its just a placeholder to store the password they enter
    10.     public string passwordToEdit = "";
    11.     void OnGUI()
    12.     {
    13.         passwordToEdit = GUI.PasswordField(new Rect(10, 10, 200, 20), passwordToEdit, "*"[0], 25);
    14.    
    15.  
    16.     // This makes a button and calls the code inside when they press it
    17.     if (GUI.Button(new Rect(30,30,200,20), "Submit"))
    18.     {
    19.             if (passwordToEdit == "Password")
    20.                 SceneManager.LoadScene(1);
    21.             // Load your Scene here
    22.         else
    23.         {
    24.             // this is just code to turn on Invalid Password, then turn it off in 5 seconds
    25.             // This line turns the Invalid Label on
    26.             ToggleLabel();
    27.             //This says turn it off in 5 seconds
    28.             Invoke("ToggleLabel",5f)
    29.         }
    30.     }
    31.  
    32.     if (showInvalid)
    33.     {
    34.         GUI.Label(new Rect(60, 60, 200, 20), "Invalid Password");
    35.     }
    36. }
    37.  
    38. void ToggleLabel()
    39.  
    40. {
    41.     showInvalid = !showInvalid;
    42. }
    43. }
    errors:
    Assets/Scripts/password.cs(29,17): error CS1525: Unexpected symbol `}'

    Assets/Scripts/password.cs(38,16): error CS1547: Keyword `void' cannot be used in this context

    Assets/Scripts/password.cs(38,17): error CS1525: Unexpected symbol `(', expecting `)', `,', `;', `[', or `='
     
  32. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    I copied the code you pasted and this is the only error I got was this line was missing a ;
    Code (CSharp):
    1.  Invoke("ToggleLabel",5f)
    Change it to:
    Code (CSharp):
    1.  Invoke("ToggleLabel",5f);
    Are you sure this code you pasted is the ONLY code in this file. I pasted this entire code into a C# script and it compiled just fine after i added that one ;