Trigger Disable help

In the game that I am making, players can open doors with points that they earn. So far, I everything is working except for my trigger
When the player enters a trigger, it displays the text “press f to open” and the cost. When the exit the trigger, the text vanishes. I want to disable the trigger when they open the door so that they no longer receive a prompt to purchase the door. However, because I have multiple doors throughout the map, I want to only disable one single trigger, not all of them.

My idea was to make the trigger a public gameobject in my script and then disable it, but this does not seem to work.

Here is the script that will control the trigger:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DoorOpenManager : MonoBehaviour
{
    public int DoorMoney;
    public GameObject Door;
    public GameObject DoorTrigger; ///here, I try to make the trigger a public gameobject

    public int PlayerPoints;


    void Update()
    {
        RaycastHit hit;
        if (Input.GetKeyDown(KeyCode.F))
        {
            if (Physics.Raycast(transform.position, transform.forward, out hit, 3.0F) && hit.transform.tag == "Door")
            {
                DoorMoney = hit.transform.GetComponent<DoorManager>().DoorCost;
                Door = hit.transform.gameObject;
               

                PlayerPoints = GetComponent<PointManager>().points;
               
                if (DoorMoney > PlayerPoints)
                {
                    Debug.Log("you cant afford this door");
                }
                else
                {
                    Debug.Log("You bought the door, congrats buddy!");
                    GetComponent<PointManager>().points = GetComponent<PointManager>().points - DoorMoney;
                    PlayerPoints = GetComponent<PointManager>().points;
                    DoorTrigger().enabled = false; // Here is where I try to disable it, but it doesn't work.
                    hit.transform.gameObject.GetComponent<Animation>().Play();
                    hit.transform.gameObject.GetComponent<BoxCollider>().enabled = false;

                   
                    GetComponent<PointManager>().ScoreText.text = "" + PlayerPoints;



                }
            }
        }
    }
}

Any ideas?

I have also tried “DoorTrigger.enabled = false” but it still doesnt work.

I don’t know how you’ve coded everything, so I’m just kinda guessing based on how I’d do it…

You’ve got code here where you do a raycast to check if they’re near the door, or whatever. You have the door in the “hit” variable. Why can’t you just use this to disable the trigger? Is the trigger not attached to the door itself??

I don’t know if you’re overcomplicating things, or if I’m just not understanding what you need to do well enough. But it seems like you should be able to attach this trigger to the door itself, and when they press F, and you’ve done your raycast, you just need to find the component on your hit variable that relates to the door trigger, and just disable that. There’s probably even better ways to arrange what you’re doing beyond that, but I think that’s one step closer to the “K.I.S.S.” approach :stuck_out_tongue:

Well, right now I have some text attached to my canvas and a trigger that activates and deactivates it. However, when I disable the trigger, the text enables and it is permanently on my screen. Even with the trigger disabled, the text remains.

There’s dozens of ‘good’ ways of doing this.

First off, do you just have one text field/canvas for all of your doors? You should… unless there’s some special reason why you need more than one. So really (if I understand what you’re trying to accomplish right), your trigger should just be setting that text field to whatever text you want when the player is near the door (i.e. your “Press F to buy the door” text). In this same trigger you can add an OnDisable function that sets the value to null or an empty string, or whatever. This will automatically get called when your game object gets disabled. Or in the same spot where you check if the key is F pressed, etc, etc… just clear the text there.

Besides what cstooch said i see some other possible problems. What is DoorTrigger() function.
I see a DoorTrigger variable.
Also I can’t think what a gameobject.enable does if it even exists.
I know components have enable.

But in your case you would want to disable the collider component to stop rayscasts from detecting it.

Or if DoorTrigger() is returning a component then you need to check earlier after your raycast if the dectect door trigger is enabled. If it is the do your code if it’s not then do nothing. Now you never check to see if it’s enabled before trying to open it.

But your code is not very clear. And seems to be missing relevant parts.

Ill admit that it’s very unclear, which is my fault. I also didn’t post a couple separate scripts that this one makes references to. However, I solved the issue. Thank you.

regarding what @daxiongmao was mentioning above, for deactivating a game object itself (vs. disabling/enabling a component):