Show button on spec pos and load scene

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!

You could do a straight up distance check:

button.enabled = (Vector3.Distance(player.transform, goal.transform) < 10);

or use trigger colliders:

void OnTriggerEnter() { button.enabled = true; }
void OnTriggerExit() { button.enabled = false; }

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.

using UnityEngine;
using System.Collections;

public class LoadSceneButton : MonoBehaviour {
 
    public Canvas LoadScene;

    void Start () {

        GameObject go = GameObject.Find("LoadScene");
        if (!go)
            return;
     
        LoadScene = go.GetComponent<Canvas>();
     
        if (LoadScene)
            LoadScene.enabled = false;
 
    }

    void OnTriggerEnter()
    {
        LoadScene.enabled = true;
    }

    void OnTriggerExit()
    {
        LoadScene.enabled = false;
    }
}

EDIT:

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

using UnityEngine;
using System.Collections;

public class LoadSceneButton : MonoBehaviour {
  
    public Canvas LoadScene;

    void Start () {

        GameObject go = GameObject.Find("LoadScene");
        if (!go)
            return;
      
        LoadScene = go.GetComponent<Canvas>();
      
        if (LoadScene)
            LoadScene.enabled = false;
  
    }

    void OnTriggerEnter (Collider other )
    {
        if( other.tag == "Player" )
        {
            LoadScene.enabled = true;
    }
}


    void OnTriggerExit (Collider other)
    {
        if( other.tag == "Player" )
        {
            LoadScene.enabled = false;
        }
    }
}

EDIT 2:
I forgot to make the collision a trigger…Solved

And that script is attached to a collider marked trigger?

You’re also missing the Collider parameter like so:

void OnTriggerEnter(Collider collider)
{
   LoadScene.enabled = true;
}

// same thing for Exit

Thanks for your reply!

I have solved it already, i made some silly mistakes :slight_smile:

1 Like