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
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): public class ToggleImage { public void ToggleButton() { // this just flips the image on and off; this.enabled = !this.enabled; } } Then set your button's OnClick to that function: Click the + Under the OnClick to add a new OnClick Listener Drag the Image down onto the None(Object) section Then the Function bar should become visible, click on it and navigate to the ToggleImage Function
Code (CSharp): using UnityEngine; using System.Collections; public class ToggleImage : MonoBehaviour { public class ToggleImage { public void ToggleButton() { // this just flips the image on and off; this.enabled = !this.enabled; } } }
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
is this what you mean? Code (CSharp): using UnityEngine; using System.Collections; using UnityEditor public class ToggleImage : MonoBehaviour { public void ToggleButton () { // this just flips the image on and off; this.enabled = !this.enabled; } } cus this doesnt work.
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.
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.
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): using UnityEngine; using System.Collections; using UnityEditor public class ToggleImage : MonoBehaviour { Image image; void Start() { image = GetComponent<Image>(); } public void ToggleButton() { // this just flips the image on and off; image.enabled = !image.enabled; } } 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
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.
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): using UnityEngine; using System.Collections; using UnityEditor public class ToggleImage : MonoBehaviour { SpriteRenderer spriteRenderer; void Start() { spriteRenderer = GetComponent<SpriteRenderer>(); } public void ToggleButton() { // this just flips the image on and off; spriteRenderer.enabled = !spriteRenderer.enabled; } }
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): using UnityEngine; using System.Collections; public class Password : MonoBehaviour { public string passwordToEdit = "Cinderella"; void OnGUI() { passwordToEdit = GUI.PasswordField(new Rect(10, 10, 200, 20), passwordToEdit, "*"[0], 25); } } 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
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): public class Password : MonoBehaviour { bool showInvalid = false; // You don't have to have anything in this // its just a placeholder to store the password they enter public string passwordToEdit = ""; void OnGUI() { passwordToEdit = GUI.PasswordField(new Rect(10, 10, 200, 20), passwordToEdit, "*"[0], 25); } // This makes a button and calls the code inside when they press it if (GUI.Button(new Rect(30,30,200,20), "Submit")) { if (passwordToEdit == "Password") // Load your Scene here else { // this is just code to turn on Invalid Password, then turn it off in 5 seconds // This line turns the Invalid Label on ToggleLabel(); //This says turn it off in 5 seconds Invoke("ToggleLabel",5f) } } if (showInvalid) { GUI.Label(new Rect(60, 60, 200, 20), "Invalid Password"); } } void ToggleLabel() { showInvalid = !showInvalid; }
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.
im getting all types of errors. check it out =) Code (CSharp): using UnityEngine; using System.Collections; using UnityEditor; public class Password : MonoBehaviour { bool showInvalid = false; // You don't have to have anything in this // its just a placeholder to store the password they enter public string passwordToEdit = ""; void OnGUI() { passwordToEdit = GUI.PasswordField(new Rect(10, 10, 200, 20), passwordToEdit, "*"[0], 25); } // This makes a button and calls the code inside when they press it if (GUI.Button(new Rect(30,30,200,20), "Submit")) { if (passwordToEdit == "Password") // Load your Scene here else { // this is just code to turn on Invalid Password, then turn it off in 5 seconds // This line turns the Invalid Label on ToggleLabel(); //This says turn it off in 5 seconds Invoke("ToggleLabel",5f) } } if (showInvalid) { GUI.Label(new Rect(60, 60, 200, 20), "Invalid Password"); } } void ToggleLabel() { showInvalid = !showInvalid; } 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?
Yes a silly mistake by me: Need to take out a } brace: Code (CSharp): void OnGUI() { passwordToEdit = GUI.PasswordField(new Rect(10, 10, 200, 20), passwordToEdit, "*"[0], 25); } <--------- Take this out And add it down here Code (CSharp): if (showInvalid) { GUI.Label(new Rect(60, 60, 200, 20), "Invalid Password"); } } <--- We added an extra } here }
Code (CSharp): using UnityEngine; using System.Collections; using UnityEditor; public class Password : MonoBehaviour { bool showInvalid = false; // You don't have to have anything in this // its just a placeholder to store the password they enter public string passwordToEdit = ""; void OnGUI() { passwordToEdit = GUI.PasswordField(new Rect(10, 10, 200, 20), passwordToEdit, "*"[0], 25); // This makes a button and calls the code inside when they press it if (GUI.Button(new Rect(30,30,200,20), "Submit")) { if (passwordToEdit == "Password") // Load your Scene here else { // this is just code to turn on Invalid Password, then turn it off in 5 seconds // This line turns the Invalid Label on ToggleLabel(); //This says turn it off in 5 seconds Invoke("ToggleLabel",5f) } } if (showInvalid) { GUI.Label(new Rect(60, 60, 200, 20), "Invalid Password"); } } } void ToggleLabel() { showInvalid = !showInvalid; } like this? getting more errors like this.
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
should i add all of this code to the LoadLevel: Code (CSharp): public void LoadScene(int level) { loadingImage.SetActive(true); Application.LoadLevel(level); } or just just Code (CSharp): Application.LoadLevel(level); and should i make a reference to it at the beginning?
Code (CSharp): // 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): GUI.Label(new Rect(60, 60, 200, 20), "Invalid Password"); } <--- Take this out
I would use this code as Application.LoadLevel is deprecated: At the top of your file add: Code (CSharp): using UnityEngine.SceneManagement; Then change Code (CSharp): if (passwordToEdit == "Password") // Load your Scene here to Code (CSharp): if (passwordToEdit == "Password") 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): 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.
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?
yes. There should also be one more } at the end of the file after ToggleLabel. Toggle Label is a part of Password
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)?
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.
im getting more errors. this time i cant find any brackets thats wrong: Code (CSharp): using UnityEngine; using System.Collections; using UnityEditor; using UnityEngine.SceneManagement; public class Password : MonoBehaviour { bool showInvalid = false; // You don't have to have anything in this // its just a placeholder to store the password they enter public string passwordToEdit = ""; void OnGUI() { passwordToEdit = GUI.PasswordField(new Rect(10, 10, 200, 20), passwordToEdit, "*"[0], 25); // This makes a button and calls the code inside when they press it if (GUI.Button(new Rect(30,30,200,20), "Submit")) { if (passwordToEdit == "Password") SceneManager.LoadScene(1); // Load your Scene here else { // this is just code to turn on Invalid Password, then turn it off in 5 seconds // This line turns the Invalid Label on ToggleLabel(); //This says turn it off in 5 seconds Invoke("ToggleLabel",5f) } } if (showInvalid) { GUI.Label(new Rect(60, 60, 200, 20), "Invalid Password"); } } void ToggleLabel() { showInvalid = !showInvalid; } } 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 `='
I copied the code you pasted and this is the only error I got was this line was missing a ; Code (CSharp): Invoke("ToggleLabel",5f) Change it to: Code (CSharp): 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 ;