help with weird errors

i have this code with these errors

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

public class Spawner : MonoBehaviour {

    // Groups
    public GameObject[] groups;
}
public void spawnNext () //error CS0116
{
    // Random Index
    int i = Random.Range(0, groups.Length);

    // Spawn Group at current Position
    Instantiate(groups[i],
                transform.position,
                Quaternion.identity);
}

// Use this for initialization

private void Start () { // error CS0116
        spawnNext();
    }

I try putting them in a class and a bunch more errors show up with everything else

i put this on the visual studio forums and they told me to go here

just a beginner so go easy on me

The closing bracket in line 9 closes your class, but then you declare methods that you want inside that class.
So just move that bracket all the way to the end.

Another problem is see is with your groups array. Assume you have no game objects added via the inspector. You then calculate the Random.Range() from 0 to 0, which will result in i = 0. Index 0 in an array is actually the first element, but you have no game objects in your array. Trying to instantiate that will throw you an error.
My suggestion is to addif (gropups.Length == 0) { return; } right at the start of your spawnNext() method. Note that that will silently just not spawn anything then. Which may or may not be desired.

Thanks so much! However the second CS0116 in the private void start on line 28 is still there. This is confusing me because that came preset in the script and is essential to the whole thing.I dont think this is my fault and is more a glitch. Anyway thanks for everything

One more thing, i saw this on the forum for what you should do when you get a CS0116 error, this is what happened

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

public class Spawner : MonoBehaviour
{

    // Groups
    public GameObject[]  groups;

public void spawnNext()
    {
        // Silent spawn
        if (groups.Length == 0) { return; };

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

        // Spawn Group at current Position
        Instantiate(groups[i],
                    transform.position,
                    Quaternion.identity);
    }
}

// Use this for initialization
class Program
{
    private void Start()
    {
        spawnNext(); // error CS0103 The name 'spawnNext' does not exist in the current context
    }
}
    // Update is called once per frame

That’s not the solution here.
The error CS0116 is thrown, when you declare methods outside of a class. In your case, as DaDonik mentioned, the Spawner class is closed before the Start method is declared. Instead of starting a new class, you have to move the parenthesis in line 24 all the way to the bottom of the file, so that each method is declared before this parenthesis is closing the class.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
    // Groups
    public GameObject[]  groups;
    public void spawnNext()
    {
        //...
    }

    // Use this for initialization
    private void Start()
    {
        spawnNext();
    }

    // Update is called once per frame
    private void Update()
    {
        //...
    }
 
    // Any other method in your class
 
}    // <-- This is where you have to close the class

Since you are new and asked a sensible question, even using code tags (wow such a rare sight :smile:), i’ll give you some working code here.
Approach #1 would be to let it fail if there are no prefabs added to the groups array. For this we use the assert and give it a meaningful text so we know what the hell is going on without the need for digging in the code.

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;


public class Spawner : MonoBehaviour
{
    public void spawnNext()
    {
        Assert.IsTrue(groups.Length > 0, "groups.Length must be greater than 0!");

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

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

    private void Start() { spawnNext(); }

    public GameObject[] groups;
}

Approach #2 would be to simply not spawn anything at all. For this we just leave the spawnNext method when we have nothing in our array.

using System.Collections.Generic;
using UnityEngine;


public class Spawner : MonoBehaviour
{
    public void spawnNext()
    {
        if (groups.Length == 0) { return; }

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

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

    private void Start() { spawnNext(); }

    public GameObject[] groups;
}

Approach #3 would be to add a bool return type to the spawnNext method and return false whenever we are not able to spawn anything. In this case having an empty groups array would result in returning false.

using UnityEngine;
using UnityEngine.Assertions;


public class Spawner : MonoBehaviour
{
    public bool spawnNext()
    {
        if (groups.Length == 0) { return false; }

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

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

    private void Start()
    {
        if (spawnNext() == false)
        {
            Debug.Log("Was not able to spawn anything!");
        }
    }

    public GameObject[] groups;
}

Please note that there are probably 100² more approaches to this and those 3 just show how you could make your code better/safer/etc. What is ‘best’ is always dependent on the context that code lives in and since you are new to programming, there is no larger context for now, so any of those are fine =)

This attitude is absolutely WRONG. It’s 99.9999999999% your fault and yours alone.
I can’t tell you how much time i wasted in my life as a programmer, thinking “that can’t be my fault”, “this must be a bug in the engine”, etc. In the end it’s most likely the most embarrasing, stupidest, most idiotic little thing that the programmer (you, me, the others here) did wrong. And thats fine, i bet every single person on this forum can related to that feeling.

Wow, this is just way too much. I cant express my thanks for you. This is just so helpful and amazing. Thanks so much

You’re welcome! And please take my advice, always question yourself, not the compiler that works for hundreds of thousands of people :smile:
And don’t hesitate to ask seemingly stupid questions, but try to google them first :wink: