Click here to Skip to main content
15,887,875 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
it keeps coming up with
Assets\Spawner.cs(19,6): error CS0116: A namespace cannot directly contain members such as fields or methods

.and i dont know what to do or what it means. this is my code

using UnityEngine;
using System.Collections;

public class Spawner : MonoBehaviour {


    public GameObject[] groups;
}
public void spawnNext() {

    int i = Random.Range(0, groups.Length);

 
    Instantiate(groups[i],
                transform.position,
                Quaternion.identity);
}

void Start() {

    spawnNext();
}


it is for a tetris game i found online.

this is the link for the website.

noobtuts - Unity 2D Tetris Tutorial[^]

Could someone please tell me specifically what and how to fix it very simply.

Thx

What I have tried:

i havent tried a lot and what ive done is basically fiddle around with te code but it would just keep making different errors.
Posted
Updated 16-May-23 18:40pm
Comments
[no name] 17-May-23 0:40am    
They've made it difficult (leaving out the "namespace" statement, for example) so you will become a premium members and download the source code. Go with something where you can get the source, instead of hoping the article is complete (which it rarely is). There are plenty of free and complete samples on the Unity web site.

1 solution

Namespaces contain classes; classes contain fields, methods, and so forth. If you do not specify a namespace, a default will be used for you.

So look at your code - you create a class that contains an array:
public class Spawner : MonoBehaviour {


    public GameObject[] groups;
}
You then try to declare a method:
public void spawnNext() {

    int i = Random.Range(0, groups.Length);

 
    Instantiate(groups[i],
                transform.position,
                Quaternion.identity);
}
But that is outside the class - so it cannot be a part of any class and cannot be associated with any object. So the compiler complains: it's basically saying "Hey! You can't call this - it needs to be inside a class!"

So move your curly brackets around to enclose all the methods you want in the class:
using UnityEngine;
using System.Collections;

public class Spawner : MonoBehaviour {
    public GameObject[] groups;
    
    public void spawnNext() {
    int i = Random.Range(0, groups.Length);
    Instantiate(groups[i], transform.position, Quaternion.identity);
    }
    
    void Start() {
    spawnNext();
    }
}
Now that error goes away.

You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!

And spending a little time learning to understand syntax error messages will save you a huge amount of time in future: you waited at least 3/4 hour for this reply, then your email system probably added another 10 minutes or so, plus the time it took you to type up the question once you had found this site and created an account. Chances are that you could have saved a significant chunk of that time if you knew how to read them!

I'm not saying we don't want to help you fix them - sometimes I can't see my own errors because I read what I meant to write - but fixing syntax errors is part of the job, and if you can't do it for yourself people are going to look at you as a bit weird should you get a job in the industry!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900