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. Dismiss Notice

Spawn Buttons but now?

Discussion in 'Scripting' started by inicosia99, Nov 7, 2020.

  1. inicosia99

    inicosia99

    Joined:
    Oct 15, 2019
    Posts:
    90
    Hi everyone, so I've done a script for my menu game and I spawned all the buttons level. Now I have a problem, how I can make start the right level when the player taps on a button?

    eg. I what to start the level 2 pushing the uttun of the level 2.

    Any ideas? this is the spawn code :

    Code (CSharp):
    1. for (int i = 1; i <= 20; i++)
    2.         {
    3.             button = Instantiate(icona) as GameObject;
    4.             button.transform.SetParent(panel.transform);
    5.            }
    6.  
    thank you:)
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    Code (CSharp):
    1. button.onClick.AddListener(() =>
    2. {
    3.     // I assume 'i' is the index of your scene
    4.     SceneManager.LoadScene(i);
    5. });
    Didn't test it. Maybe there's a syntax error somewhere, but this is what will work.
     
  3. inicosia99

    inicosia99

    Joined:
    Oct 15, 2019
    Posts:
    90

    Doesn't work cause each button load scene 20...
     
  4. FedeStefan

    FedeStefan

    Joined:
    Aug 15, 2018
    Posts:
    221
    Code (CSharp):
    1.  SceneManager.LoadScene (SceneManager.GetActiveScene ().name);
    2.  
     
  5. inicosia99

    inicosia99

    Joined:
    Oct 15, 2019
    Posts:
    90
    it loads the scene of the menu level...
     
  6. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @inicosia99

    "Doesn't work cause each button load scene 20..."

    "eg. I what to start the level 2 pushing the uttun of the level 2."

    Your question isn't very clear. So do you mean by "uttun" a button? And you want to assign each button a number, so button 1, 2 and 3 will load scenes with index 1, 2 and 3?

    If you want to assign some int value to each of your button listener anonymous method, you can't do that simply using variable i in your for loop. You'll have to have a local variable in your for loop, to which you assign/capture the value of i.

    Otherwise you'll end up with all buttons having the last value of i in for loop.

    Do a google about C# closures and for loops.
     
  7. inicosia99

    inicosia99

    Joined:
    Oct 15, 2019
    Posts:
    90
    there should be a way to attach
    Okay, Thank you. Yes, I did a type error, "uttun" means button.

    So I looked at some stuff but I really don't know where to start.
     
  8. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    " I really don't know where to start."

    Did you actually read all the above answers and my answer?
     
  9. inicosia99

    inicosia99

    Joined:
    Oct 15, 2019
    Posts:
    90
    yes duh.... I looked about closures but I don't understand how to use it
     
  10. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    But did you actually try to create a for loop similar to what @Dextozz described...?

    It alone won't work, but I tried to explain how to fix the issue.

    Anyway. Instead of saying you don't understand, could you show your current best effort code.
     
  11. inicosia99

    inicosia99

    Joined:
    Oct 15, 2019
    Posts:
    90
    I can't cause I didn't understand how to use the closures!!
     
  12. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    But did you read what I wrote?

    "If you want to assign some int value to each of your button listener anonymous method, you can't do that simply using variable i in your for loop. You'll have to have a local variable in your for loop, to which you assign/capture the value of i."
     
    Dextozz likes this.
  13. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    Yes. I also came across this a few months back. I was wondering if this was going to be an issue in my example above. Anyhow, this should completely fix any issues you have.