Search Unity

can someone correct my script

Discussion in 'Scripting' started by thesimi, Aug 30, 2009.

  1. thesimi

    thesimi

    Joined:
    Jun 16, 2009
    Posts:
    94
    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):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour {
    6.  
    7. int lose = 0
    8.    
    9. OnTriggerEnter (Collider){
    10.     // code that +1 to lose. when a colider hits the trigger
    11. }
    12.    
    13. // If lose = more than 7
    14. if (lose > 7){
    15.  
    16.         function OnGUI () {
    17.  
    18.             GUI.Box (Rect (10,10,100,90), "Game Over");
    19.             // GUI button
    20.             if (GUI.Button (Rect (20,40,80,20), "Restart")) {
    21.                     Application.LoadLevel (2);
    22.             }
    23.             // GUI button
    24.             if (GUI.Button (Rect (20,70,80,20), "Main Menu")) {
    25.                 Application.LoadLevel (0);
    26.             }
    27.         }  
    28.     }
    29. }
    30.  
    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
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    The most obvious thing wrong is that the if statement can't be outside the OnGUI function, so instead of:-
    Code (csharp):
    1. if (lose > 7) {
    2.     function OnGUI() {
    3.         // GUI stuff goes here...
    4.     }
    5. }
    6.  
    ...you need to do something like:-
    Code (csharp):
    1. function OnGUI() {
    2.     if (lose > 7) {
    3.         // GUI stuff goes here...
    4.     }
    5. }
    6.  
     
  3. thesimi

    thesimi

    Joined:
    Jun 16, 2009
    Posts:
    94
    Thanks a lot
    forgot that :oops:

    but, can you help me with this

    Code (csharp):
    1.  
    2. OnTriggerEnter (Collider){
    3.    // code that +1 to lose. when a colider hits the trigger
    4. }
    I don't know what to whrite in the middle
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    If you're adding 1 each time, you can just write
    Code (csharp):
    1. lose++;
    ...in the function body. To add any other number (or a value from a variable), you can write
    Code (csharp):
    1. lose += 5;
     
  5. thesimi

    thesimi

    Joined:
    Jun 16, 2009
    Posts:
    94
    Thanks a lot, but it says I have an parsing error

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour {
    6.  
    7. int lose = 0
    8. // the error is in the next line (8,1)    
    9. OnTriggerEnter (Collider){
    10. lose++;
    11. }
    12.    
    13. // If lose = more than 7
    14.  function OnGUI() {
    15.         if (lose > 7) {
    16.                 GUI.Box (Rect (10,10,100,90), "Game Over");
    17.             // GUI button
    18.             if (GUI.Button (Rect (20,40,80,20), "Restart")) {
    19.                 Application.LoadLevel (2);
    20.             }
    21.             // GUI button
    22.             if (GUI.Button (Rect (20,70,80,20), "Main Menu")) {
    23.                 Application.LoadLevel (0);
    24.             }
    25.         }
    26.     }
    27. }
    28.  
     
  6. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    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):
    1. function OnTriggerEnter(other: Collider) {
    2.     lose++;
    3. }
     
  7. thesimi

    thesimi

    Joined:
    Jun 16, 2009
    Posts:
    94
    Thank you so much for helping me, but it still says parsing error

    heres the code

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour {
    6.  
    7. int lose = 0
    8.    
    9. function OnTriggerEnter(other: Collider) {
    10.     lose++;
    11. }
    12.    
    13. // If lose = more than 7
    14.  function OnGUI() {
    15.         if (lose > 7) {
    16.                 GUI.Box (Rect (10,10,100,90), "Game Over");
    17.             // GUI button
    18.             if (GUI.Button (Rect (20,40,80,20), "Restart")) {
    19.                 Application.LoadLevel (2);
    20.             }
    21.             // GUI button
    22.             if (GUI.Button (Rect (20,70,80,20), "Main Menu")) {
    23.                 Application.LoadLevel (0);
    24.             }
    25.         }
    26.     }
    27. }
    28.  
     
  8. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    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):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NewBehaviourScript : MonoBehaviour {
    5.  
    6.     int lose = 0;
    7.    
    8.     void OnTriggerEnter(Collider other) {
    9.         lose++;
    10.     }
    11.      
    12.     void OnGUI() {
    13.         if (lose > 7) {
    14.             GUI.Box (new Rect (10,10,100,90), "Game Over");
    15.            
    16.             // GUI button
    17.             if (GUI.Button (new Rect (20,40,80,20), "Restart")) {
    18.                 Application.LoadLevel (2);
    19.             }
    20.            
    21.             // GUI button
    22.             if (GUI.Button (new Rect (20,70,80,20), "Main Menu")) {
    23.                 Application.LoadLevel (0);
    24.             }
    25.         }
    26.     }
    27. }
     
  9. thesimi

    thesimi

    Joined:
    Jun 16, 2009
    Posts:
    94
    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.