I’m coding this for an elevator and i want the button when clicked on the first floor to bring me to the second floor, there is is no worry about going back down, just up for the moment. Any help ASAP would be greatly appreciated! Thanks guys!
If the button you press is a 3D object in the game, you could use something like this:
if(Input.GetMouseButtonDown(0) == true)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float rayLength = 500.0f;
RaycastHit hitInformation;
if(Physics.Raycast(ray, out hitInformation, rayLength) == true)
{
// Do some kind of check to make sure its the right button.
// It doesn't need to be the name, it can be anything really.
if(hitInformation.transform.name == yourButtonObjectName)
{
Application.LoadLevel(LevelIndex);
return;
}
}
}
If the user presses the left mouse button, a ray is created to see if the user is clicking on your specific 3d object button. And if there is a hit, then you load the scene you want.
If it is a 2D GUI button you have, then it’s more easy.
if (GUI.Button(Rect(10,70,50,30),"2") == true)
{
Application.LoadLevel(LevelIndex);
return;
}
Good luck!
Thank you so much, just applied this to my code, worked fine, my highest praise goes your way right now!