Search Unity

Bug Why are there errors when my code seems fine?

Discussion in 'Editor & General Support' started by mUSTARD_, Sep 30, 2021.

  1. mUSTARD_

    mUSTARD_

    Joined:
    Sep 6, 2021
    Posts:
    1
    Hi, I feel like I've run into a brick wall of code because no matter what i do, this script gives me errors that don't seem to make sense. As far as i can see, there isn't actually anything wrong. Visual studio gives me no errors but there are 2 in unity itself.
    The unity editor gives me errors cs1513: } expected and cs1022: Type or namespace definition, or end-of-file expected

    Here is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class move : MonoBehaviour {
    7.     private Rigidbody rb;
    8.     public float movementSpeed = 10f;
    9.     public static int Collectibles;
    10.     private GameObject PlayerSphere;
    11.     public TextMeshProUGUI countText;
    12.     public TextMeshProUGUI countHP;
    13.     private float HP = 3f;
    14.  
    15.    
    16.  
    17.    
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.        
    22.         rb = GetComponent<Rigidbody>();
    23.         Collectibles = 0;
    24.         HP = 3f;
    25.         setCountText();
    26.         setCountHP();
    27.         return;
    28.     }
    29. //unity complains about this next line
    30.     void Awake() {
    31.         public Vector3 CurrentScale = (1.0f, 1.0f, 1.0f);
    32.     }
    33.        
    34.  
    35.     void setCountText()
    36.     {
    37.         countText.text = Collectibles.ToString();
    38.     }
    39.     void setCountHP()
    40.     {
    41.         countHP.text = HP.ToString();
    42.     }
    43.  
    44.     void Update()
    45.     {
    46.         if (CurrentScale.x > 5f || CurrentScale.y > 5f || CurrentScale.z > 5f)
    47.         {
    48.             TooBig = true;
    49.         }
    50.         else
    51.         {
    52.             TooBig = false;
    53.         }
    54.         while (TooBig == false)
    55.         {
    56.             //transform.localScale += Vector3 CurrentScale(GiftCards + 0.1f, GiftCards + 0.1f, 0.2f);
    57.  
    58.         }
    59.        
    60.     }
    61.     // Update is called once per frame
    62.     void FixedUpdate()
    63.     {
    64.         if(HP < 0.5f)
    65.             {
    66.                 countHP.text = ("You're Dead!");
    67.                 OnPlayerDeath();
    68.  
    69.             }
    70.        
    71.        
    72.  
    73.     }
    74.     public void OnPlayerDeath()
    75.     {
    76.         countHP.text = ("Dead");
    77.  
    78.  
    79.        
    80.  
    81.     }
    82.     void OnTriggerEnter(Collider other)
    83.     {
    84.         if(other.CompareTag("Deadly"))
    85.         {
    86.             HP = HP -1;
    87.             setCountHP();
    88.             //gameplayManager.updateHealth(HP);
    89.         }
    90.            
    91.  
    92.            
    93.  
    94.  
    95.            
    96.         if (other.CompareTag("Collectible"))
    97.         {
    98.             other.gameObject.SetActive(false);
    99.             Collectibles= Collectibles+ 1;
    100.             setCountText();
    101.            
    102.         }
    103.    
    104.     }
    105. //and this last line
    106. }
    107.  
    I appreciate any and all help.
     

    Attached Files:

  2. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    797
    The error message is pointing you to line 31. It's doing its best to work out what the problem is, but it doesn't know your intention.

    You are declaring a public variable inside the Awake method, which is not allowed. You'll have to remove "public" and declare that variable outside of the method if you want to use it elsewhere.
     
    Joe-Censored likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Remember: the compiler is always right.

    The complete error message contains everything you need to know to fix the error yourself.

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    ALSO: Remember that NOBODY memorizes error codes. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    When you are monkey-hammer-banging code in from the internet, keep this in mind to save a LOT of time:

    How to do tutorials properly:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly. Fortunately this is the easiest part to get right.

    Be a robot. Don't make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE.

    If you get any errors, learn how to read the error code and fix it. Google is your friend here. Do NOT continue until you fix the error. The error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    When you declare a variable inside a method it will be a local variable to that method only. You cannot declare these local variables with qualifiers like "public", "private", etc. You use those when you declare a variable outside any method.