Search Unity

Question Bodies not constructing properly

Discussion in 'Getting Started' started by wowYouAreGood, Apr 24, 2022.

  1. wowYouAreGood

    wowYouAreGood

    Joined:
    Aug 22, 2021
    Posts:
    1
    When I saved my C# code, I came up with 2 errors in the Unity Editor:
    "} expected"
    "Type or namespace definition, or end-of-file expected"

    I do not see ANY errors that could have popped up.
    Here's my code.
    Code (CSharp):
    1.   using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Button2 : MonoBehaviour
    6. {
    7.     private static Button2 instance;
    8.     public static Button2 SharedInstance {
    9.     get
    10.     {
    11.       if (instance == null)
    12.       {
    13.          instance = new Button2();
    14.       }    
    15.       return instance;
    16.     }
    17.     public int start;
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.        
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         if (Input.GetMouseButtonDown(0) == true)
    28.         {
    29.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    30.             RaycastHit hit;
    31.             if (Physics.Raycast(ray, out hit))
    32.             {
    33.                 if (hit.transform.name == "Square")
    34.                 {
    35.                  start = 1;
    36.                 }
    37.             }
    38.         }
    39.     }
    40. }
    41.  
    What happened?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    You always want to look at the line numbers of the errors, along with the text. That will help you know where to search. Since you didn't share the line numbers, it's a little hard to figure out, but I'd guess your main issue is around lines 8 - 16. You have a property declaration that is missing its closing
    }
    It's a little hard to see because you did not indent the contents of the property appropriately.

    Second thing to note is that you likely don't have your IDE configured properly, which would let you see this error underlined inside your code editor. I would get on configuring your IDE ASAP, it will make your life a lot easier.