Question What am I doing wrong?

Voxeu

New member
Joined
Sep 28, 2022
Messages
1
Programming Experience
Beginner
I'm trying to do a project for my coding class, and I need help. I've been at this for almost an hour and can't figure out why it won't work. I am working with unity and it's Visual Studio Code. What I want to happen is whenever I enable my cube, it says Creating enemy number: and then it counts to five.
My current errors are these:
C#:
[10:26:24] Assets\Scripts\Enemies.cs(16,13): error CS0128: A local variable or function named 'numEnemies is already defined in this scope
[10:26:24] Assets\Scripts\Enemies.cs(18,19): error CS0136: A local or parameter named 'numEnemies' cannot be declared in this scope because that name is used in an enclosing local scope to define a local parameter
[10:26:24] Assets\Scripts\Enemies.cs(16,13): warning CS0219: The variable 'numEnemies is assigned but it's value is never used

My current code is this:
C#:
{
   // for ( ; ; ) {}

    void OnEnable ()
    {

        int numEnemies = 5;

        Debug.Log("Creating enemy number: " + numEnemies);

        for ( int numEnemies = 0; numEnemies < 5; numEnemies++ )
        {
            Debug.Log ( numEnemies );
        }

    }
}
If you could please lead me in the right direction it would be greatly appreciated. Thank you for reading! :3
 
Last edited by a moderator:
The errors and warnings are telling you exactly what the issue is. You are declaring the variable numEnemies on line 7, and then you are trying to declare another variable named numEnemies on line 11. The warning is due to you declaring and initializing the variable on line 7, but never using it.

If you don't want the errors, use different variable names. If you don't want the warning, actually use the variable you declared, or don't even declare the variable.
 
The variable 'numEnemies is assigned but it's value is never used

What I really want to know is how the second apostrophe came to be shifted 19 places to the right. Never seen the compiler make such a heinous grammatical error.. :)
 
I suspect the OP has poorly formatted code and has some very deep lines. Some people don't really care about indentation consistency.
 
Back
Top Bottom