error on script [Solved]

void Awake() {
int x = list.Next(list.Count);
Debug.LogError(x);
return x;
//Probably a good idea to run an IEnumerator and yield until
//all game objects are added to list, but for now I’ll just place this here:
foreach (GameObject gameObject in GameObject.FindObjectsOfType(typeof(GameObject)))
{
Bloques.Add(gameObject);
}
}

  1. Use code tags

  2. Always paste the full error message, and generally paste the full script in too, especially if you don’t have any idea what an error message means.

That error message usually means that you have mismatched brackets { } somewhere above where the error is. e.g. it thinks you’re trying to declare a function (void) but according to the brackets you’re still inside another function.

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

public class test : MonoBehaviour {

    public List<GameObject> Bloques = new List<GameObject>();

    static Random list = new Random();
    void Start()
    {
    switch (Bloques)
        {
            case "base":

                break;
            case "1":

                break;
            case "2":

                break;
            case "3":

                break;
            case "4":

                break;
            case "5":

                break;
            case "6":

                break;
            case "7":

                break;
            case "8":

                break;
        }

         void Awake()     {
            int x = list.Next(list.Count);
            Debug.LogError(x);
            return x;
            //Probably a good idea to run an IEnumerator and yield until
            //all game objects are added to list, but for now I'll just place this here:
            foreach (GameObject gameObject in GameObject.FindObjectsOfType(typeof(GameObject)))
        {
            Bloques.Add(gameObject);
        }
    }

 

        }

         public GameObject GetGameObject_FromBloques(string name)
    {
        foreach (GameObject gameObject in Bloques)
        {
            if (gameObject.name == name)
            {
                return gameObject;
            }
        }
        Debug.LogError("Game Object not found in Bloques!");
        return null;
        }
}

You awake function is within your start function. You need to check your closing brackets. Which is what @StarManta said.

Also… good lord, that indentation. It’s no wonder you’re having trouble keeping track of your brackets, I wouldn’t be able to either.

If you’re using MonoDevelop or VS, they should have options to automatically indent your code that should make it a lot easier to keep track of this sort of thing.

where is that option lol and ty brathman xd

Which editor?

vs community 2017

I think part of it is VS tends to sometimes break your bracket alignment when you mess them up. This is usually a good indication that you have something not right.

In VS 2017 (and I think mono has this), they have a dashed line from opening to closing bracket. So if you follow an opening line down and don’t see a closing bracket lined up with it where it should be, you might have a missing bracket issue.

Otherwise, VS is normally pretty good with indentations.

A quick thing you can do when writing, if you brackets misalign/break, is that (if you’ve fixed them, so there are the right number), is to just delete the bottom one and re-add it, and the IDE will auto-correct the positions for you.
You can also highlight a bracket to find its matching pair… in case you get “lost” in a mess :wink:

hey
can u help me with this xd?

should pick 1 element of the list of gameobject and then make the switch statment whe u click but dont do anything xdd

Sure I already answered this some time ago in another thread.

Input.GetMouseButtonDown is pretty useless in start…that’s why it doesn’t work.

lol i didnt see it jaja ty i will put on update

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

public class test : MonoBehaviour {

    public List<GameObject> Bloques = new List<GameObject>();
    static System.Random list = new System.Random();
    public int x = list.Next(9);

   
    void Update()
    {
        if (Input.GetMouseButtonDown(0)) { 
        switch (x)
        {
            case 0:
                Debug.Log("0");
                break;
            case 1:
                Debug.Log("1");
                break;
            case 2:
                Debug.Log("2");
                break;
            case 3:
                Debug.Log("3");
                break;
            case 4:
                Debug.Log("4");
                break;
            case 5:
                Debug.Log("5");
                break;
            case 6:
                Debug.Log("6");
                break;
            case 7:
                Debug.Log("7");
                break;
            case 8:
                Debug.Log("8");
                break;
        }
        }
    }

        void Awake()     {
   


}

 

       

    /*public GameObject GetGameObject_FromBloques(string name)
    {
        foreach (GameObject gameObject in Bloques)
        {
            if (gameObject.name == name)
            {
                return gameObject;
            }
        }
        Debug.LogError("Game Object not found in Bloques!");
        return null;
        }*/
}

still without work

only do the case 1 …

I didn’t even think that would compile without a default.
You aren’t fetching a .Next() random when you click the mouse.

Defaults aren’t required, generally just encouraged to also have something to fall to. But if you will never get a value you aren’t checking for, you don’t really need one.

@PachiGG As @methos5k said. You only generate your random number once, so you will always get the same result.

Very interesting. I did not know that. :slight_smile:

Looking into this, I just read about c# 7’s new additions to switch statements lol (outside of Unity’s scope for now, but cool to learn)…