Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How can I apply "Application.LoadLevel" to a custom made button?

Discussion in 'Scripting' started by Drewson, Jul 24, 2011.

  1. Drewson

    Drewson

    Joined:
    Apr 19, 2009
    Posts:
    168
    I have a static background image on a poly plane.

    Floating on top of that, I have a small poly plane with a button texture on it.

    I want to write a script that I can apply to the button that will load a level of my choice when the button is tapped/clicked.

    Can someone tell me what to write?
     
  2. TheChronicPhenix

    TheChronicPhenix

    Joined:
    Jan 14, 2010
    Posts:
    874
    I'd do a raycast from the camera, to wherever the player taps/clicks, and if it hits the button, then you can just call Application.LoadLevel.
     
  3. Drewson

    Drewson

    Joined:
    Apr 19, 2009
    Posts:
    168
    So my scene uses a single Orthograpic main camera, all my art is 2D. The camera looks straight at the main card with the button cards floating in front of the background.

    What is the simplest way to achieve this ability to load another level based on where the user touches the screen / clicks?

    I would greatly appreciate a code sample - I am not a scripter and am kind of clueless unless I have a starting point to go from. :)
     
  4. Drewson

    Drewson

    Joined:
    Apr 19, 2009
    Posts:
    168
    There are other things I want to be able to trigger when the user clocks / touches things on screen, like animations, audio clips etc.

    Can I use something like Raycast.Collider to do all of this functionality?

    I feel like I am close to understanding,
     
  5. TheChronicPhenix

    TheChronicPhenix

    Joined:
    Jan 14, 2010
    Posts:
    874
    Heres and example (untested)
    Code (csharp):
    1.  
    2. function Update(){
    3. if(Input.GetButtonDown("Fire1")){
    4. var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    5. var hit : RaycastHit;
    6. if(Physics.Raycast(ray, hit)){
    7. if(hit.collider.tag("button")){
    8. Application.LoadLevel(0);
    9. }
    10. }
    11. }
    12. }
     
  6. zine92

    zine92

    Joined:
    Nov 13, 2010
    Posts:
    1,347
    If you are using GUI.Button()
    Code (csharp):
    1.  
    2. function OnGUI()
    3. {
    4.     if(GUI.Button(Rect(10, 10, 30, 30), "Next Level"))
    5.     {
    6.         Application.LoadLevel(1);
    7.     }
    8. }
    9.  
    Or if you are using an object like a cube or something with a collider.

    Just use this
    Code (csharp):
    1.  
    2. function OnMouseDown()
    3. {
    4.     Application.LoadLevel(0);
    5. }
    6.  
     
  7. Drewson

    Drewson

    Joined:
    Apr 19, 2009
    Posts:
    168
    So the whole script should read:

    "

    using UnityEngine;
    using System.Collections;

    public class NewBehaviourScript : MonoBehaviour
    {

    function OnMouseDown()
    {
    Application.LoadLevel(0);
    }

    }

    ?

    It isn't functioning. I am not sure how or where to embed your script (looks like exactly what I want though :)
     
  8. Drewson

    Drewson

    Joined:
    Apr 19, 2009
    Posts:
    168
    I got it working for loading levels. Thanks!

    Now, to trigger an animation? Or audio?
     
  9. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    It's called the docs. I suggest you start reading them cos we're not going to code your game for you.
     
  10. macdude2

    macdude2

    Joined:
    Sep 22, 2010
    Posts:
    686
    function OnMouseDown(){
    audio.Play();
    animation.Play();
    }
     
  11. Drewson

    Drewson

    Joined:
    Apr 19, 2009
    Posts:
    168
    It's not a game, and the help I was asking for is about the extent of what I am trying to accomplish in total. Sorry for offending you, feel free not to read my posts.
     
  12. Drewson

    Drewson

    Joined:
    Apr 19, 2009
    Posts:
    168
    Thanks to those who are helping this artist get a little functionality going.

    I am noticing a little change though - today I am replacing my OnGUI buttons with actual collider elements. While using true GUI buttons, touch (on iPad) and click (with mouse) both worked.

    Now that I am using the OnMouseDown function with colliders, mouse click works, but tapping on iPad no longer triggers functionality.

    Is this a simple fix or do I have to get into iOS library stuff to make this work?

    Thanks for your time. :)