Author Topic: Depending on which button is clicked, load a specific level  (Read 6296 times)

stingman

  • Guest
Hey, I wanted to know how I can achieve this with NGUI.  I have 50 buttons on my menu.  Each button will load a specific level in the game if it is clicked.  But I don't want to write 50 different scripts relating to each button.  This is my idea... but it's not working.  Is there an easy way to go about doing this... like if "this button is clicked"?

using UnityEngine;
using System.Collections;

public class MenuButtonLoadLevel : MonoBehaviour {
   
   public GameObject[] button;
   
   void OnClick() {
      
      if (button[0])
         Application.LoadLevel(3);
      if (button[1])
         Application.LoadLevel(4);
      if (button[2])
         Application.LoadLevel(5);
      if (button[3])
         Application.LoadLevel(6);
      if (button[4])
         Application.LoadLevel(7);
      if (button[5])
         Application.LoadLevel(8);
}
}


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Depending on which button is clicked, load a specific level
« Reply #1 on: May 19, 2012, 10:22:37 PM »
  1. using UnityEngine;
  2.  
  3. [AddComponentMenu("Game/Load Level on Click")]
  4. public class LoadLevelOnClick : MonoBehaviour
  5. {
  6.         public string levelName;
  7.  
  8.         void OnClick ()
  9.         {
  10.                 if (!string.IsNullOrEmpty(levelName))
  11.                 {
  12.                         NGUITools.Broadcast("End");
  13.                         Application.LoadLevel(levelName);
  14.                 }
  15.         }
  16. }