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

how to fix my error

Discussion in 'Scripting' started by pinkpenguindev, Sep 5, 2020.

  1. pinkpenguindev

    pinkpenguindev

    Joined:
    Aug 13, 2020
    Posts:
    43
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class levelTrans : MonoBehaviour
    6. {
    7.  
    8. private ghost[] = _ghosts;
    9.  
    10.     void onEnable()
    11.    {
    12.        _ghosts = Find ObjectsOfType<ghost>();
    13.    }
    14.     void Update()
    15.     {
    16.        
    17.     }
    18. }
    19.  
    Assets\levelTrans.cs(8,18): error CS1519: Invalid token '=' in class, struct, or interface member declaration
    Assets\levelTrans.cs(12,23): error CS1002: ; expectedAssets\levelTrans.cs(8,27): error CS1519: Invalid token ';' in class, struct, or interface member declaration
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
  3. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,497
    You have a space between "Find" and ObjectsOfType"
     
  4. pinkpenguindev

    pinkpenguindev

    Joined:
    Aug 13, 2020
    Posts:
    43
  5. pinkpenguindev

    pinkpenguindev

    Joined:
    Aug 13, 2020
    Posts:
    43
  6. DaDonik

    DaDonik

    Joined:
    Jun 17, 2013
    Posts:
    258
    We are really trying to help you here, but you are not making it easy for us.

    First of all, this is your second thread that is titled 'how to fix my error'. This is pretty much as undescriptive as you can get. Please be a bit more descriptive next time.

    As for your code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class levelTrans : MonoBehaviour
    5. {
    6.     // I guess you are trying to create an array here,
    7.     // but like@PraetorBlue said, it's not done correctly.
    8.     private ghost[] = _ghosts;
    9.  
    10.     // If you are trying to use the OnEnable method the
    11.     // MonoBehaviour class provides you, you have to
    12.     // actually write OnEnable and not onEnable.
    13.     void onEnable()
    14.     {
    15.         // Like already mentioned by @polemical, you should
    16.         // write Find.ObjectsOfType<ghost>() here.
    17.         // Your code editor should actually underline that part
    18.         // and tell you that you have messed it up.
    19.         _ghosts = Find ObjectsOfType<ghost>();
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.     }
    25. }

    It could look something like this:
    Code (CSharp):
    1. // In a file named ghost.cs
    2. using UnityEngine;
    3.  
    4. public class ghost : MonoBehaviour
    5. {
    6.     // Whatever your doing in that class.
    7. }
    8.  
    9. // In a file named levelTrans.cs
    10. using UnityEngine;
    11.  
    12. public class levelTrans : MonoBehaviour
    13. {
    14.     // Define an array of indeterminate size that
    15.     // can hold instances of the ghost class.
    16.     private ghost[] ghostArray;
    17.  
    18.     void OnEnable()
    19.     {
    20.         // Initialize the array with all the ghosts that can be found.
    21.         ghostArray = FindObjectsOfType<ghost>();
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.         // Do something for each ghost we found.
    27.         for (int i = 0; i < ghostArray.Length; i++)
    28.         {
    29.             // Just move each ghost to the global right (x+ direction)
    30.             // 1 unit per second.
    31.             ghostArray[i].transform.position += Vector3.right * Time.deltaTime;
    32.         }
    33.     }
    34. }
    35.  

    Please make sure to use a decent code editor. I always recommend JetBrains Rider. It's not free (after the 30day free trial), but imho it is simply the best one for C# development. It will point out most of the errors you have had until now and often point you to the solution.
     
    Last edited: Sep 5, 2020
  7. pinkpenguindev

    pinkpenguindev

    Joined:
    Aug 13, 2020
    Posts:
    43
    i put it in and a error Assets\levelTrans.cs(10,1): error CS1529: A using clause must precede all other elements defined in the namespace except extern alias declarations
     
  8. DaDonik

    DaDonik

    Joined:
    Jun 17, 2013
    Posts:
    258
    My comments tell you that the code i posted is meant to be in two seperate files. The error you get tells me that you have not read them...
     
    bobisgod234, Yoreki and PraetorBlue like this.
  9. pinkpenguindev

    pinkpenguindev

    Joined:
    Aug 13, 2020
    Posts:
    43
  10. DaDonik

    DaDonik

    Joined:
    Jun 17, 2013
    Posts:
    258
    I almost feel sorry to say that, but i don't want to invest any more of my precious time in helping someone who puts in as little effort as you do.

    Good luck.
    Maybe someone else is bored enough to spoonfeed you, but i doubt it.
     
    Kurt-Dekker likes this.
  11. pinkpenguindev

    pinkpenguindev

    Joined:
    Aug 13, 2020
    Posts:
    43
  12. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    You need to separate DaDonik's example into 2 files. This is explained in the comments of that file. If you do not do this, then you get the exact error message you are getting. (Yeah, I'm bored :p)

    Try to do that.

    Are you trying to follow a tutorial or something? Might be worth posting a link to the tutorial if you are having problems with it.
     
    DaDonik likes this.
  13. pinkpenguindev

    pinkpenguindev

    Joined:
    Aug 13, 2020
    Posts:
    43
    yes i can here
     
  14. DaDonik

    DaDonik

    Joined:
    Jun 17, 2013
    Posts:
    258
    Hey i know that youtube channel.
    I was bored :rolleyes: and could not believe that he would show code that is as broken as the one you have posted.
    And i was right!
    He explains it very detailed and shows pretty much the exact code you have, for some reason, failed to clone.
    Check out this time stamp, you can see the code you have posted, minus the error.

    Long story short, pay attention to detail, because programming is all about the details.
    If you are wondering about something, google it. It that raises 10 new questions, google them.
    This will be your life for the forseeable future.
    Many of us are so old, we would have killed multiple people, just to have google as a learning resource. Use it!
    Over and out.
     
    bobisgod234 likes this.
  15. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Seriously, this ^ ^ ^. I still have some of the games I wrote in high school in 1984. That software is still running (in my KurtMaster2D game for all targets, interpreted by a modern C assembly "runner" I made) and I still can't believe I sussed out how to do 8085 bitmapped code on the TRS-80 Model 100 laptop, all without anything even remotely like google. It's nutty... and before that for me it was Apple II and Vic-20 stuff with 6502 assembly, but I don't have any of that code anymore, alas. I wish I did! So much PEEK, POKE and CALL going on... wow.

    And this too: capitalization, spelling and punctuation, as well as syntax (ordering of the tokens you type) are critical. There are no exceptions. If you don't like that, perhaps programming is a poor career choice.
     
  16. kaparlagus

    kaparlagus

    Joined:
    Sep 11, 2021
    Posts:
    4
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;


    public class QuestionsData : MonoBehaviour
    {
    public questions;
    [SerializeField] private Text _questionText;

    void Start()
    {
    AskQuestion();
    }
    public void AskQuestion()
    {
    if (CountValidQuestions() == 0)
    {
    _questionText.text = string.Empty;
    ClearQuestion();
    SceneManager.LoadScene("MainMenu");
    return;
    }

    var randomIndex = 0;
    do
    {
    randomIndex = UnityEngine.Random.Range(0, questions.questionsList.Count);
    } while (questions.questionsList[randomIndex].questioned == true);

    questions.currentQuestion = randomIndex;
    questions.questionsList[questions.currentQuestion].questioned = true;
    _questionText.text = questions.questionsList[questions.currentQuestion].question;
    }

    public void ClearQuestion()
    {
    foreach (var question in questions.questionsList)
    {
    question.questioned = false;
    }
    }
    private int CountValidQuestions()
    {
    int validQuestions = 0;

    foreach (var question in questions.questionsList)
    {
    if (question.questioned == false)
    validQuestions++;
    }

    Debug.Log("Questions Left " + validQuestions);
    return validQuestions;
    }
    }
     
  17. kaparlagus

    kaparlagus

    Joined:
    Sep 11, 2021
    Posts:
    4
    can you help me guys eror cs1519
     
  18. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,895
    A: Make your own thread, rather than necro-post a year old thread.
    B: Use Code tags, and...
    C: Simple compiler errors shouldn't warrant a forum post. Use your IDE to locate the error and fix it. Refer to the C# documentation if you are unsure of the correct syntax.
     
  19. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Please don't necro old threads for your own fatfinger typos. You can fix those yourself, here is how:

    Remember: 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.

    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.