Home > Software design >  Unity C#: why am I getting error CS1022: Typenamespace definition, or end-of-file expected, trying t
Unity C#: why am I getting error CS1022: Typenamespace definition, or end-of-file expected, trying t

Time:01-07

I do not believe I have any missing semicolon or missing/ double brackets which is what I see others saying causes this error. Thank you for reading.

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

public class Spawner : MonoBehaviour
{
    public float moveX;
    public float moveXIncrease;
    public GameObject greenSquare;
    public GameObject blueSquare;
    public GameObject pinkSquare;
    public GameObject greySquare;
    
    var squareslist = new List<GameObject>{greenSquare, blueSquare, pinkSquare, greySquare};
    
void Start()
     {
        for (int i = 0; i < 5; i  )
        {
            int index = random.Next(squareslist.Count);
            private GameObject objectToSpawn = squareslist[index];
            
            Vector3 newPos = new Vector3(objectToSpawn.transform.position.x   moveX, objectToSpawn.transform.position.y, objectToSpawn.transform.position.z);
            Instantiate(objectToSpawn, newPos, objectToSpawn.transform.rotation);
            moveX = moveX   moveXIncrease;
        }
     }
}

I am trying to spawn 5 different random colored blocks next to each other.

CodePudding user response:

You cannot declare a private scope within a function.

Within your Start() function, remove the private keyword. Change this line...

private GameObject objectToSpawn = squareslist[index];

... to this...

GameObject objectToSpawn = squareslist[index];

MS documentation...

private (C# Reference)

CodePudding user response:

In addition to the answer given by @quaabaam, you can’t use the var keyword at the class level scope.

So to fix the two errors:

var squareslist = new List<GameObject>{greenSquare, blueSquare, pinkSquare, greySquare};
    
void Start()
{
    for (int i = 0; i < 5; i  )
    {
        int index = random.Next(squareslist.Count);
        private GameObject objectToSpawn = squareslist[index];
            
        Vector3 newPos = new Vector3(objectToSpawn.transform.position.x   moveX, objectToSpawn.transform.position.y, objectToSpawn.transform.position.z);
        Instantiate(objectToSpawn, newPos, objectToSpawn.transform.rotation);
        moveX = moveX   moveXIncrease;
    }
}

should be:

private List<GameObject> squareslist;
    
void Start()
{
    squareslist = new List<GameObject>{greenSquare, blueSquare, pinkSquare, greySquare};
    for (int i = 0; i < 5; i  )
    {
        var index = random.Next(squareslist.Count);
        var objectToSpawn = squareslist[index]; 
        var newPos = new Vector3(objectToSpawn.transform.position.x   moveX, objectToSpawn.transform.position.y, objectToSpawn.transform.position.z);
        Instantiate(objectToSpawn, newPos, objectToSpawn.transform.rotation);
        moveX  = moveXIncrease;
    }
}

There’s now another issue. Prefabs don’t store position. Your attempt at reading the position from your list of Prefabs won’t produce the result you’re looking for. If you want to set “spawn points”, you might want to add empty GameObjects to your scene, and use those positions for your newly instantiated GameObjects. But that’s another issue, unrelated to this initial question.

  • Related