Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Can't add a void to void start

Discussion in 'Scripting' started by tGuevarra, Jun 15, 2022.

  1. tGuevarra

    tGuevarra

    Joined:
    Jun 13, 2021
    Posts:
    10
    Code (CSharp):
    1.   void Start()
    2.     {
    3.         GetButtons();
    4.         AddListeners();
    5.         AddGameMemos();
    6.         gameGuesses = gameMemos.Count / 2;
    7.         Shuffle(gameMemos);
    8.     }
    9. ...
    10. void Shuffle (List<Sprite> list)
    11.         {
    12.             for (int i = 0; i < list.Count; i++)
    13.             {
    14.                 Sprite temp = list[i];
    15.                 int randomIndex = Random.Range(0, list.Count);
    16.                 list[i] = list[randomIndex];
    17.                 list[randomIndex] = temp;
    18.             }
    19.         }
    Can someone explain, all other "voids" are working well and i can add them to void Start(), but shuffle isn't, when i try to place it ahead it can recognize a "Shuffle void", but it doesn't work
     
  2. tGuevarra

    tGuevarra

    Joined:
    Jun 13, 2021
    Posts:
    10
    int randomIndex = Random.Range(i, list.Count); change 0 to i and it's works well, but still have problem with location in script, works only if i adds shuffle void before start
     
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    First of all those are not "voids". They are called methods. You wouldn't call your car "a red" or "a black" because it has the color red / black, would you? void is the return type of the method and void is a special pseudo type which indicates "nothing".

    Anyways What exactly do you mean be "working" or "recognize"? If something is not recognized it would mean that your code does not compile. Is that the case? Do you get a compiler error? If so what is it. If you don't get a compiler error the method is recognised since you call it in line 7. You can't call something that does not exist / is not recognised.

    If you don't get any compiler error, the next question would be, do you get any runtime error? So when you start your game, do any errors pop up in the console? If so, which ones?

    If you don't get any compiler nor runtime errors we would interpret your "not working" as a logical error. So you expect X to happen but you actually get Y (Y could be nothing at all).

    I just read your second post (came in intermediately). Yes to do a proper equally likely shuffle you have to use "i" as the start value for your random value. However it does shuffle with 0 as well, just that the shuffle would be biased. Though you still haven't said what is the actual issue? I would guess that you actually use your list in some other method and you expect the list to be shuffled there but you actually called your shuffle method afterwards. Of course you have to call it at a place where it does what you want.

    Imagine we play a game with cards (in real life). The deck of cards is not shuffled in the beginning, So all cards are ordered. We wouldn't draw our playing cards before we shuffle the deck, would we? Because shuffling the deck after we have drawn our cards would of course not change the cards we have already drawn. This is of course just an analogy. We don't know what you do with the elements in your list. Since you have some other methods that you call in Start, like "GetButtons()" or "AddGameMemos()", those may use the elements in the list to do something. Again shuffling the list after you used the list will most likely not change the outcome of what you have done before the shuffle.

    You really should be more clear about your actual issue.
     
  4. tGuevarra

    tGuevarra

    Joined:
    Jun 13, 2021
    Posts:
    10
    Sure a bit chaotic thoughts, thanks for help, and sure this is methods. That's not actual a card game, so I need this shuffle at start, that's why I'm asking. But anyway when I write this method after "Start()", it get compiler error "method doesn't exist", so if I write it above "Start()", it works well. But I got a bunch of methods below, that's I'm wondering why it doesn't work.
     
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    This doesn't make any sense. It doesn't matter where inside the class you place a method declaration. Maybe you placed it outside the class? That's not possible. You can not define methods outside a class in C#.

    In the code snippet you've shown you placed it right after the Start method. So this would work. Are you sure there is not another closing curly bracket in between?