Search Unity

Show button on spec pos and load scene

Discussion in 'Scripting' started by cruising, Jul 4, 2015.

  1. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    Hello!

    How to show(activate a button (canvas one) when a player enters a position to load scene on klick?

    I know how to make a button functional like that, its just how to make it show up when player enters the pos within a specific distance.

    Thanks for your help!
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You could do a straight up distance check:
    Code (csharp):
    1.  
    2. button.enabled = (Vector3.Distance(player.transform, goal.transform) < 10);
    3.  
    or use trigger colliders:
    Code (csharp):
    1.  
    2. void OnTriggerEnter() { button.enabled = true; }
    3. void OnTriggerExit() { button.enabled = false; }
    4.  
     
  3. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329

    Thanks for your reply again!

    I made it like this and added it to the door who should trigger it, but i bet i missing some code to make it work since it dont work when the player comes close to the door.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LoadSceneButton : MonoBehaviour {
    5.  
    6.     public Canvas LoadScene;
    7.  
    8.     void Start () {
    9.  
    10.         GameObject go = GameObject.Find("LoadScene");
    11.         if (!go)
    12.             return;
    13.      
    14.         LoadScene = go.GetComponent<Canvas>();
    15.      
    16.         if (LoadScene)
    17.             LoadScene.enabled = false;
    18.  
    19.     }
    20.  
    21.     void OnTriggerEnter()
    22.     {
    23.         LoadScene.enabled = true;
    24.     }
    25.  
    26.     void OnTriggerExit()
    27.     {
    28.         LoadScene.enabled = false;
    29.     }
    30. }
    EDIT:

    Made the code like this and added a cube and unchecked collider and mesh, added the script to it, but still dont work.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LoadSceneButton : MonoBehaviour {
    5.  
    6.     public Canvas LoadScene;
    7.  
    8.     void Start () {
    9.  
    10.         GameObject go = GameObject.Find("LoadScene");
    11.         if (!go)
    12.             return;
    13.      
    14.         LoadScene = go.GetComponent<Canvas>();
    15.      
    16.         if (LoadScene)
    17.             LoadScene.enabled = false;
    18.  
    19.     }
    20.  
    21.     void OnTriggerEnter (Collider other )
    22.     {
    23.         if( other.tag == "Player" )
    24.         {
    25.             LoadScene.enabled = true;
    26.     }
    27. }
    28.  
    29.  
    30.     void OnTriggerExit (Collider other)
    31.     {
    32.         if( other.tag == "Player" )
    33.         {
    34.             LoadScene.enabled = false;
    35.         }
    36.     }
    37. }
    EDIT 2:
    I forgot to make the collision a trigger......Solved
     
    Last edited: Jul 5, 2015
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    And that script is attached to a collider marked trigger?

    You're also missing the Collider parameter like so:

    Code (csharp):
    1.  
    2. void OnTriggerEnter(Collider collider)
    3. {
    4.    LoadScene.enabled = true;
    5. }
    6.  
    7. // same thing for Exit
    8.  
     
  5. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329

    Thanks for your reply!

    I have solved it already, i made some silly mistakes :)
     
    GroZZleR likes this.