so, I read a book abaut C#, to figure out one of the last scripts for my game, but I havent learned everything I need. I made a script that needs corection, and misses some words. can someone corect it??? heres the script (C#) Code (csharp): using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { int lose = 0 OnTriggerEnter (Collider){ // code that +1 to lose. when a colider hits the trigger } // If lose = more than 7 if (lose > 7){ function OnGUI () { GUI.Box (Rect (10,10,100,90), "Game Over"); // GUI button if (GUI.Button (Rect (20,40,80,20), "Restart")) { Application.LoadLevel (2); } // GUI button if (GUI.Button (Rect (20,70,80,20), "Main Menu")) { Application.LoadLevel (0); } } } } PS: The book is 400 pages long, so if I have to learn everything before I can finish the game, it will take a long time before I can finish, due to school, homework and friends
The most obvious thing wrong is that the if statement can't be outside the OnGUI function, so instead of:- Code (csharp): if (lose > 7) { function OnGUI() { // GUI stuff goes here... } } ...you need to do something like:- Code (csharp): function OnGUI() { if (lose > 7) { // GUI stuff goes here... } }
Thanks a lot forgot that but, can you help me with this Code (csharp): OnTriggerEnter (Collider){ // code that +1 to lose. when a colider hits the trigger } I don't know what to whrite in the middle
If you're adding 1 each time, you can just write Code (csharp): lose++; ...in the function body. To add any other number (or a value from a variable), you can write Code (csharp): lose += 5;
Thanks a lot, but it says I have an parsing error Code (csharp): using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { int lose = 0 // the error is in the next line (8,1) OnTriggerEnter (Collider){ lose++; } // If lose = more than 7 function OnGUI() { if (lose > 7) { GUI.Box (Rect (10,10,100,90), "Game Over"); // GUI button if (GUI.Button (Rect (20,40,80,20), "Restart")) { Application.LoadLevel (2); } // GUI button if (GUI.Button (Rect (20,70,80,20), "Main Menu")) { Application.LoadLevel (0); } } } }
OnTriggerEnter is actually a function, so you need to declare it with the function keyword. Also, Collider is the name of a class, not a parameter:- Code (csharp): function OnTriggerEnter(other: Collider) { lose++; }
Thank you so much for helping me, but it still says parsing error heres the code Code (csharp): using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { int lose = 0 function OnTriggerEnter(other: Collider) { lose++; } // If lose = more than 7 function OnGUI() { if (lose > 7) { GUI.Box (Rect (10,10,100,90), "Game Over"); // GUI button if (GUI.Button (Rect (20,40,80,20), "Restart")) { Application.LoadLevel (2); } // GUI button if (GUI.Button (Rect (20,70,80,20), "Main Menu")) { Application.LoadLevel (0); } } } }
Sorry, for some reason I didn't notice - you've got a mixture of C# and JavaScript there! You don't use the word "function" to declare functions in C#. If the function returns a value, you use the type of that value (int, float, etc) or if it returns nothing, you use the word "void". Also, you have to use the "new" operator when you create an instance of a class or struct. Your code should be:- Code (csharp): using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { int lose = 0; void OnTriggerEnter(Collider other) { lose++; } void OnGUI() { if (lose > 7) { GUI.Box (new Rect (10,10,100,90), "Game Over"); // GUI button if (GUI.Button (new Rect (20,40,80,20), "Restart")) { Application.LoadLevel (2); } // GUI button if (GUI.Button (new Rect (20,70,80,20), "Main Menu")) { Application.LoadLevel (0); } } } }
Thank you so much, you realy helped, I have been working in weeks, to make it work, and you helped me out in under 2 hours, I will mention you in the credits.