Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question i got error cs1002 ; expected in a c# can someone help me

Discussion in 'Scripting' started by greenboy_1392, Oct 20, 2023.

  1. greenboy_1392

    greenboy_1392

    Joined:
    Oct 19, 2023
    Posts:
    3
    i tried but it did nothing or break the rules of syntax and cause more errors this is the code

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;

    public class goal : MonoBehaviour
    {
    private void OnTriggerEnter(Collider other)
    {
    PlayerController component = other gameObject.GetComponent<PlayerController>();
    if(component != null)
    {
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }
    }
    }
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,261
    First of all, please use code tags when posting code. There are two buttons in the toolbar, one for adding a block of code and one for
    inline code snippets
    .

    Second when you get a compiler error, the error tells you the exact line number and even character number in the error. You have not shared that information. The compiler can tell you exactly where the error is. We have now search through your complete code to find the error again. So please include the relevant information next time.

    Your error is in this line:

    Code (CSharp):
    1. PlayerController component = other gameObject.GetComponent<PlayerController>();
    You're missing a "dot" in between "other" and gameObject. As it's written right now, with a space, it just makes no sense. The compiler is not an AI that can predict what you actually wanted to do. It just reads what you have written and when it doesn't make sense it complains about that. Lexically that space makes no sense to the compiler, so it suggests that you may by missing a semicolon which terminates the previous statement. However that's not what you want. You want

    Code (CSharp):
    1. PlayerController component = other.gameObject.GetComponent<PlayerController>();
    or just

    Code (CSharp):
    1. PlayerController component = other.GetComponent<PlayerController>();
    since every component also has a GetComponent method so there's no need to manually grab the gameobject.
     
  3. greenboy_1392

    greenboy_1392

    Joined:
    Oct 19, 2023
    Posts:
    3
    thanks but it made more errors
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,661
    We can't quite see your screen from here to read the new errors.
     
    Nad_B and Bunny83 like this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    You can fix your own typing mistakes. Here's 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.

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

    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.
     
    Bunny83 likes this.
  6. greenboy_1392

    greenboy_1392

    Joined:
    Oct 19, 2023
    Posts:
    3
    sorry for a month with out a reply the error code was a name of a file i spelled wrong