- Home /
Button Enabling/Disabling using Collision Triggers?
Hello! I'm working on a simple Application. I want to have 2 overlapping buttons, one of which will add a point to your score, and the other one will bring you to the next scene(Game Over Screen). I already have the coding for the point system and the scene switch. I want these buttons to alternate being Enabled and Disabled based off of a continuously rotating object's collision with a trigger at the top of its path(GreenZone). So if the rotating object is inside the GreenZone, (Enable pointButton, Disable loseButton) and if it's outside, (Disable pointButton, Enable loseButton). That's the only issue I'm having getting this done and I'm at a complete loss on what to do. I want to know if this is the best way to do something like this, and if so, how! Thanks, guys<3
Answer by davidcox70 · Apr 18, 2018 at 06:59 PM
If I understand correctly, you should add a collider to a gameObject that covers the green zone area. You should then set that collider to be a trigger. Triggers only work if one of the colliding gameobjects have a rigidbody component attached to them. So add it if you don't have one. Then in a script attached to the gameObject that has that collider trigger, add something like;
void OnTriggerEnter(Collider col){
// object is in the green zone, set the buttons (for example...)
GameObject.Find("PointsButton").GetComponent<Button>().interactable=false;
GameObject.Find("GameOverButton").GetComponent<Button>().interactable=true;
}
void OnTriggerExit(Collider col){
// object has left the green zone, set the buttons (for example...)
GameObject.Find("PointsButton").GetComponent<Button>().interactable=true;
GameObject.Find("GameOverButton").GetComponent<Button>().interactable=false;
}
That works perfectly for detecting the collision. I am, however, getting this message in the console upon playing: "NullReferenceException: Object reference not set to an instance of an object", along with the issue of my ("pointButton") not functioning at all. $$anonymous$$y ("loseButton") works perfectly, but if I click when the object is inside the trigger nothing happens when it should be adding a point to my score. Thank you for the help!
The null exception means that the GameObject.Find line is not finding your button. It then fails to get the button component because it doesn't know what game object to find it on.
Either the name in the GameObject.Find(name) does not match the game object name your button is attached to, or it is being set as inactive before the GameObject.Find can locate it.
Either double check your names, or use a different method to locate the target buttons. For example., you could add this at the beginning on the script;
public GameObject pointButton;
public GameObject loseButton;
This will make pointButton and loseButton appear in the property inspector of the script when you look in the editor window. You can then drag your buttons from the hierarchy into these items and this will form the reference. Then you don't need to use "Find" to get a reference to the button objects, you can simply use;
pointsButton.GetComponent<Button>().interactable=true;
The error is happening here: "UnityEditor.Graphs.Edge.WakeUp () (at C:/buildslave/unity/build/Editor/Graphs/UnityEditor.Graphs/Edge.cs:114)" and I don't know why or how to fix it. It will only let me press the button that is in the front. Do you know how to translate the buttons to a specific point on the z axis rather than adding to the value of the point it's already at? I was using transform.Translate(0, 0, -1) and (0, 0, 1) but that wasn't the desired effect. Thank you
Your answer
Follow this Question
Related Questions
1 Button Alternating Between 2 onClick Functions 1 Answer
Unlocking & "Equipping" Colors onto Sprites with c# 2 Answers
Using c# to Set a Button's Color onto a GameObject in Another Scene? 1 Answer
Coin Counter still not working after 5+ hours of frustrating changing code and moving stuff around 2 Answers