Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Im Getting These errors and i dont know what to do.

Discussion in 'Scripting' started by McXbox, Jul 13, 2023.

  1. McXbox

    McXbox

    Joined:
    Dec 2, 2022
    Posts:
    2
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Tut_Door_Open : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         public GameObject Tut_Door;
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         void OnTriggerEnter()
    17.         {
    18.             Destroy.GameObject(Tut_Door);
    19.         }
    20.     }
    21. }
    22.  
    the error codes are Assets\Scripts\Tut_Door_Open.cs(9,6): error CS1513: } expected

    Assets\Scripts\Tut_Door_Open.cs(14,5): error CS8803: Top-level statements must precede namespace and type declarations.

    Assets\Scripts\Tut_Door_Open.cs(21,1): error CS1022: Type or namespace definition, or end-of-file expected
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    This is clearly nothing to do with 2D so please look at the available forums before posting.

    I'll move your post to the Scripting forum for you. and remove the 2D tag.
    Thanks.
     
  3. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    483
    Line 10 -
    public GameObject Tut_Door;
    should be above your
    Start
    , not inside the method.
    Line 16 to 19 - your entire
    OnTriggerEnter
    method should not be inside your
    Update
    method. Move it to be outside your
    Update
    method.

    Based on these mistakes, I recommend that you go through a load of the Learn lessons as these are fundamentals of C# programming (not specific to Unity).
     
  4. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    You are declaring a function within the Update() function. Move the OnTrigger function outside the Update() brackets.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,561
    You are just making TONS of typing mistakes.

    You do not need our help to fix your typing. You can do that yourself. Here's how:

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

    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)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    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.

    Remember: NOBODY here memorizes error codes. That's not a thing. 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.

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    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 step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    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 your error. Google is your friend here. Do NOT continue until you fix your error. Your 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. If you want to learn, you MUST do Step 2.

    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!

    Finally, when you have errors, don't post here... just go fix your errors! See the top of this post.
     
  6. McXbox

    McXbox

    Joined:
    Dec 2, 2022
    Posts:
    2
    I put 2D because my game is 2D, if that caused confusion sorry, I am very new to this stuff.
     
  7. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    521
    I will hazard a guess that even if you studied these forums for a year you would be confused as to where to post. I can never be certain where something belongs either. o_O
     
    Kurt-Dekker likes this.
  8. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,082
    Just to expand a little more on this one as it's interesting: you can declare nested methods in C#. You just won't be able to call them from outside of the method they're inside, and Unity's system for finding magic methods won't be able to find them and thus they will never execute.
     
  9. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    Interesting. Can't think of a situation in which it will be helpful yet, but who knows, I just started with Unity and C#, still a lot to explore.

    Declaring a function within the Update() function is still a bad idea I guess
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    It's not a problem, I moved your post for you. We get a lot of posts in 2D about UI or any kind of problem when making a 2D game but we have specific feature forums available for those and others. This is why we inform devs that it's the wrong forum otherwise you'd never know.

    The scripting forum if you're asking about generic scripts, the physics forum if you're asking about physics, the audio forum if about audio, the 2D forum if you're asking about a 2D feature etc.

    The Getting Started forum if you're just getting started too.

    Thanks.
     
  11. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Local methods are a feature of C# you probably will seldom use. The only situation where I use them with any frequency is for methods that take a delegate (usually a predicate). For example:
    Code (CSharp):
    1. public static int FirstEven(this IEnumerable<int> ints)
    2. {
    3.     return intList.FirstOrDefault(FirstEvenPredicate);  
    4.  
    5.     bool FirstEvenPredicate(int value)
    6.     {
    7.         return value % 2 == 0;
    8.     }
    9. }
    Pretty silly example, but they can be useful for writing little chunks of reusable code that are only relevant in the one method, or a particular scope.
     
    Ryiah and ijmmai like this.
  12. CodeRonnie

    CodeRonnie

    Joined:
    Oct 2, 2015
    Posts:
    280
    I think I remember thinking of local methods as potentially useful for having a public API method which is the actual entry point to a recursive private method. In the case that the public method is the only one you would ever actually call directly, it performs some necessary initial setup, and then calls into the actual recursive method, and that local recursive method doesn't need to be called from anywhere else. I hope I'm remembering that correctly as a reason I thought they might be useful for making the code just seem a bit cleaner to myself. But, of course you can accomplish the same thing with just a separate private method.
     
    spiney199 likes this.
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,561
    It can be very helpful... in much the same way that lambda functions are helpful: it keeps things tidy.

    Code (csharp):
    1. void B()
    2. {
    3.   void A()
    4.   {
    5.   }
    6.   A();
    7. }
    If you have function A declared inside of function B, then nobody outside of B ever knows about A, and thus cannot be tempted to use it or meddle with its state.

    Not quite... a private function at the class level still cannot have private state... but if you make it private, now every invocation of the containing function (function B in my example above) can have variables unique to function A, as long as those variables are defined in function B's scope.

    It's not that it's a bad idea. It's the fact that those "internal" functions simply are not part of the class. With your original code above, Unity would ask your class (via reflection) "do you have an OnTriggerEnter() method?" and the class would say "Nope!" because it doesn't have that method. The class cannot "see" methods hiding inside of methods at that same level.

    Anyway, glad you're sorted... almost everything in a program must be where it must be or bad things happen.
     
  14. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    It wasn't my problem, but thanks for taking the time to confirm what others said before.

    I did get that there are situation in which declaring a function within a function can be the way to go. Within the Update(), declaring it over and over again with every frame, seemed weird, that is all.
     
  15. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    521
    A quick note but it isn't actually "declaring a function" each time Update is called. The code is compiled. As has been noted it shouldn't be there however.