- Home /
1 Button Alternating Between 2 onClick Functions
I want a single button to carry out ONLY ONE of two onClick functions at a time. The timing will be determined by an OnTriggerEnter2D/OnTriggerExit2D script attached to the trigger of an orbiting game object constantly entering and exiting, at the top of its path, the trigger. I want the button to add a point(which is one of my onClick functions), but only if the game object is inside the trigger. If it's outside the trigger, I want that same button to bring you to the Game Over Scene(the other one of my onClick functions). How can I enable and disable these onClick functions based off the collision detection so that it works smoothly? I appreciate the help!
Answer by Kciwsolb · Apr 20, 2018 at 01:43 AM
Have the button call a function that calls one of two different functions depending on some bool value. Like this pseudo-ish code:
public bool isInTrigger;
private void DoThingWhenButtonPressed() //Called by button's onClick
{
if(isInTrigger)
{
DoThing1(); //Add to score?
}
else
{
DoThing2(); //Game Over?
}
}
Then just set the bool value on a script on the GameObject that has your Trigger. Maybe use OnTriggerEnter and set it to true and OnTriggerExit and set it to false. Hope this helps! :)
This is my code so far. It works, but it wont update the text on screen with the score counting up in the script. Any fixes? Thanks!
~
public class GreenBand : $$anonymous$$onoBehaviour { public Collider2D Greenband; public bool isInTrigger; public UnityEngine.UI.Text score;
void Update()
{
score.text = "" + startScore;
}
void OnTriggerEnter2D(Collider2D Greenband)
{
Debug.Log("Object is Inside Trigger");
isInTrigger = true;
}
void OnTriggerStay2D(Collider2D Greenband)
{
Debug.Log("Object is Inside Trigger");
isInTrigger = true;
}
void OnTriggerExit2D(Collider2D Greenband)
{
Debug.Log("Object is Outside Trigger");
isInTrigger = false;
}
public int startScore = 0;
public int plusOne = 1;
void pointButton()
{
if (isInTrigger)
{
startScore += plusOne;
}
else
{
Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex + 1);
}
}
}
I think you are misusing the OnTrigger methods. They are methods called by the physics system when two colliders / triggers overlap. When they are called, the physicals system will pass in a Collider2D (that you can call what ever you want in your implementation of them) for you to use. You do not want to be calling it Greenband as that is the name of the reference you have at the top of your script. You probably want to call it "other" or "collider2D" or something. Then you want to compare that collider with your referenced Collider2D above to see if they are the same. If they are, then you can execute the rest of the code.
void OnTriggerEnter2D(Collider2D other)
{
if(other == Greenband)
{
Debug.Log("Collider we care about is inside Trigger");
isInTrigger = true;
}
}
Do the same for OnTriggerExit2D. You really don't need OnTriggerStay2D. It is really only a performance drain in this case. You only need to check when something enters and when something leaves the trigger to see if it is the Collider2D you care about.
It wasn't the trigger functions, long story short, I'm an idiot hahaha. I had another score script constantly updating the same text back to 0 but I fixed it! Thanks so much, man
Your answer
Follow this Question
Related Questions
Button Enabling/Disabling using Collision Triggers? 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