- Home /
Disable the EventTrigger or Box Collider via C# Script
Hello guys,
im doing a learning programm to educate how to work with real cameras with unity. My problem is, I have a power button on the camera which you have to press to start the game. I have an Event Trigger on it with a changing mouse script which changes the mouse to a hand and back when hovering over the power button of the modelled camera. And here is the point, I have no clue how to disable the Event Trigger or the Box Collider within the script which says whats happening after pressing the power button (the dark blue one, called ramp_button).
No other thread I've found about that topic worked for me.
I want the result of not letting the mouse change whenever I hover over the power button on the camera in my scene ever again after pressing the power button. I know i cant do it by disabling the hovermouse script.
picture for references:
Thanks in advance!
We can only offer generalities without some of the code you're saying has a problem.
Basically the mouse changes when you hover, so you need a bool defining the power on state. When power has been turned on, simply skip the hover's mouse changing code.
Answer by Strixie13 · Jul 22, 2018 at 01:03 AM
gameObject.GetComponent<BoxCollider>().enabled = false;
gameObject.GetComponent<BoxCollider>().isTrigger = false;
First one disables the entire box collider, second one removes the trigger. Just disabling the box should work, as it can't trigger if the component is disabled.
thanks that worked. Now the next step would be what do I have to replace "gameObject" with to deactivate a BoxCollider on a specific Object which isnt paired with the script? So ins$$anonymous$$d "de/activate BoxCollider of object called 'power on' in scene"?
Edit: Got it, I guess its "GameObject.Find("GameObjectName").GetComponent()"
The gameObject
will return whatever object the script is attached to. To "find" an object you can make a public GameObject objectName
variable then drag and drop to store it. Or you can find it at runtime using GameObject.FindGameObjectWithTag("tagname").GetComponent<BoxCollider>();
$$anonymous$$ake sure to add a tag in the inspector. If the collider isn't directly on the main object, you may need to use GetComponentInChildren ins$$anonymous$$d.
Answer by kalen_08 · Jul 22, 2018 at 02:14 AM
If you no longer need the component then why not just remove it!?
Destroy(gameObject.GetComponent<Collider2D> ());
this will solve the problem