How to make a UI Button who opens a new level depending on a selected object?
I'm developping an architectural application in unity and i would like to create a unique Ui button that opens a specific level depending on the selected object (actually the objects are 2d plans of 9 appartments and each appartment have a unique corresponding level in wich player can do an interior walkthrough). I do not want to make a button for each appartment, neither click on one of the plans to directly access the corresponding level, but i would like to select one of the plans then click on the button to access the level corresponding to this plan. Can you help me please?
Thanks.
on each room you add a script that, on the button, change a variable to the name of the scene to load, and on the button, you just use Application.LoadLevel(levelName), with levelName the name of the level modified by the scripts on the rooms.
Answer by askri78 · Nov 03, 2015 at 10:20 PM
Thanks for the answer. Ok so i've been able to make a script attached to the button that uses a variable to load the desired level. I also wrote the script that have to be attached to the selected object (actually the appartment 2d plan) in wich i declare the same public variable that will be used in the button script. Now the problem is i don't know how to communicate this variable between the to scripts. @Fire_Cube
you can use Gameobject.Find("theGameobjectThatContainTheButton").GetComponent().levelToLoad = the level to load;
with you're message, i understand you declare a variable with the level to load on each script, but i won't be the same var in the two. I'm not sure i have been clear, but i don't have much time now, i re write the answer in 3-4 hours
Thank you very much. It works now. Actually I used the script that keeps informations about the appartment (like area, orientation, etc) and added a public string variable wich is the reference for the appartment. This script is attached to each appartment gameobject and sends informations to the canvas. The canvas displays these informations as well as the level button when in game mode i click on one of the appartments. Now i attached a new script to this button wich gathers the appartment name variable from the canvas and loads the corresponding level (actually the level name of each appartment is the same as the appartment name string variable so the LevelLoad script uses the text information in the canvas to load the level).
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class LevelLoad : $$anonymous$$onoBehaviour
{
private string RefLot;
private Transform LtoLoad;
public Canvas InfoCanvas;
public void LevelClick ()
`` {
LtoLoad = InfoCanvas.transform.Find("infoPanel/Haut/lot");
RefLot = LtoLoad.GetComponent<Text>().text;
Application.LoadLevel (RefLot);
}
}
I really don't know if there is a simplest way to reach the same result.