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

Please Help If You Can

Discussion in 'Scripting' started by drdunson, Mar 7, 2022.

  1. drdunson

    drdunson

    Joined:
    Oct 31, 2019
    Posts:
    3
    I am v3ry new to Unity. I've come a long way in the past 6 months, but this is a new level of complexity for me, and I am not sure what to do. I am not sure how to post the code that I am struggling with, so I'll just post it and you can instruct me as to how to post code questions in the future.

    I keep getting the following error: Assets\MyScripts\EnemyAttack.cs(96,5): error CS8803: Top-level statements must precede namespace and type declarations.

    I simply do not know how to fix it. Thank you in advance!


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.AI;
    public class EnemyAttack : MonoBehaviour
    {
    private NavMeshAgent Nav;
    private NavMeshHit Hit;
    private bool blocked = false;
    private bool RunToPlayer = false;
    private float DistanceToPlayer;
    private bool IsChecking = true;
    private int FailedChecks = 0;
    [SerializeField] Transform Player;
    [SerializeField] Animator Anim;
    [SerializeField] GameObject Enemy;
    [SerializeField] float MaxRange = 35.0f;
    [SerializeField] int MaxChecks = 3;
    [SerializeField] float ChaseSpeed = 4.0f;
    [SerializeField] float WalkSpeed = 1.50f;
    [SerializeField] float AttackDistance = 2.0f;
    [SerializeField] float AttackRotateSpeed = 2.0f;
    [SerializeField] float CheckTime = 3.0f;
    [SerializeField] GameObject ChaseMusic;
    // Start is called before the first frame update
    void Start()
    {
    Nav = GetComponentInParent<NavMeshAgent>();
    ChaseMusic.gameObject.SetActive = false;
    }
    // Update is called once per frame
    void Update()
    {
    DistanceToPlayer = Vector3.Distance(Player.position, Enemy.transform.position);
    if (DistanceToPlayer < MaxRange)
    {
    if (IsChecking == true)
    {
    IsChecking = false;
    blocked = NavMesh.Raycast(transform.position, Player.position, out hit, NavMesh.AllAreas);
    if (blocked == false)
    {
    Debug.Log("I can see the player.");
    RunToPlayer = true;
    FailedChecks = 0;
    }
    if (blocked == true)
    {
    Debug.Log("Where did the player go?");
    RunToPlayer = false;
    Anim.SetInteger("State", 1);
    FailedChecks++;
    }
    StartCoroutine(TimedCheck());
    }
    }
    if (RunToPlayer == true)
    {
    Enemy.GetComponent<EnemyMove>().enabled = false;
    ChaseMusic.gameObject.SetActive = true;
    if (DistanceToPlayer > AttackDistance)
    {
    Nav.IsStopped = false;
    Anim.SetInteger("State", 2);
    Nav.acceleration = 24;
    Nav.SetDestination(Player.position);
    Nav.speed = ChaseSpeed;
    }
    if (DistanceToPlayer < AttackDistance)
    {
    Nav.IsStopped = true;
    Anim.SetInteger("State", 3);
    Nav.acceleration = 180;
    }
    }
    else if (RunToPlayer == false)
    {
    Nav.IsStopped = true;
    }
    }
    }

    IEnumerator TimedCheck()
    {
    yield return new WaitForSeconds(CheckTime);
    IsChecking = true;
    if (FailedChecks > MaxChecks)
    {
    Enemy.GetComponent<EnemyMove>().enabled = true;
    Nav.IsStopped = false;
    Nav.speed = WalkSpeed;
    FailedChecks = 0;
    ChaseMusic.gameObject.SetActive = false;
    }
    }
     
  2. getmyisland_dev

    getmyisland_dev

    Joined:
    Dec 22, 2020
    Posts:
    100
    Please use code tags when posting code. This makes it much easier for people to read and you will get help sooner. Please refer to this post.

    Also try figuring out the exact problem or use the name of the error and change your title.

    Have you tried googling the error?
     
  3. jbnlwilliams1

    jbnlwilliams1

    Joined:
    May 21, 2019
    Posts:
    267
    Well, very difficult to diagnose without code tags, but it looks like you may not have closed the class properly, i do not see the closing "}" for

    public class EnemyAttack : MonoBehaviour
    {

    Again, difficult to tell witout code tags.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,767
    This means "YOU ARE MAKING TYPOS."

    The compiler is telling you how to fix your typos.

    You must bring your typing accuracy up to 100%, not even one character wrong.

    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.

    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:

    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 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!
     
  5. drdunson

    drdunson

    Joined:
    Oct 31, 2019
    Posts:
    3
    Thank you both so much! I was feeling like Robinson Crusoe yesterday. This is very helpful!
     
  6. Ritvik_Nellutla

    Ritvik_Nellutla

    Joined:
    Jul 14, 2022
    Posts:
    17
    Hello,
    I am very new to Unity and this is my first project I am trying to make. I keep getting this error, and I am not understanding why that is so. Here is the error is says, and below is my code.


    ERROR - Assets\Scripts\Collectables\CollectCoin.cs(11,9): error CS8803: Top-level statements must precede namespace and type declarations.

    CODE -
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class CollectCoin : MonoBehaviour
    {
    public AudioSource coinFX;

    void OnTriggerEnter(Collider other);
    }
    coinFX.Play();
    this.gameObject.SetActive(false);
    }
    {



    Please see if you can help,
    Thank you!
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,767
    Nope, it's all your typing mistakes.

    For instance, you have a curly bracket { and } that is facing the wrong way.

    You can fix your own typing mistakes. Here is how:

    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.

    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.

    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.

    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... start back at the top of this post!
     
  8. Ritvik_Nellutla

    Ritvik_Nellutla

    Joined:
    Jul 14, 2022
    Posts:
    17
    Can you please tell me what specifically I should fix?
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,767
    Did you even read what I wrote?

    If that's not enough to figure it out, then go look at ANY SCRIPT IN THE WORLD and see how those things work.
     
  10. Ritvik_Nellutla

    Ritvik_Nellutla

    Joined:
    Jul 14, 2022
    Posts:
    17
    Thank you so much for your response and your tips! They're really useful! Can you please tell me what exactly I need to change?
     
  11. Ritvik_Nellutla

    Ritvik_Nellutla

    Joined:
    Jul 14, 2022
    Posts:
    17
    Hello,
    I am very new to Unity and this is my first project I am trying to make. I keep getting this error, and I am not understanding why that is so. Here is the error is says, and below is my code.


    ERROR - Assets\Scripts\Collectables\RotateObject.cs(5,14): error CS0101: The namespace '<global namespace>' already contains a definition for 'RotateObject

    CODE -
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class RotateObject : MonoBehaviour
    {
    public int rotateSpeed = 1;

    void Update()
    {
    transform.Rotate(0, rotateSpeed, 0, Space.World);
    }
    }




    Please see if you can help,
    Thank you!
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,767
    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.

    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.

    It looks like you have two scripts named RotateObject. Delete one of them.

    ALSO, please don't necro-post old threads for silly typos. If you have a question, make a new post... it's FREE!

    When you post, remember nobody can read your mind.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    If you EVER post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
    hkalterkait likes this.
  13. Ritvik_Nellutla

    Ritvik_Nellutla

    Joined:
    Jul 14, 2022
    Posts:
    17
    Okay, thank you! Also, I am getting this error, please help me.
    Internal build system error. Backend exited with code -1073741819.
     
  14. danwazue

    danwazue

    Joined:
    Aug 19, 2022
    Posts:
    1
    Bro this isn't an actual answer
     
  15. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,767
    Which is perfect, because what was typed above isn't an actual program!

    You will know it's an actual program when it works and has no errors.

    My suggestions above can aid you, or you're welcome to use your own approach if you prefer.
     
    Last edited: Oct 5, 2022