- Home /
Text not hiding...
So I wrote this script, and when I press "Action" the WeaponSelectionQuestionText won't go away. The player input detection below it (the weapon wall weapon selection input) doesn't work either. I am guessing the fix above it will fix them both, otherwise can you please help me with that too?
if (other.gameObject.name == "Player") {
WeaponSelectQuestionText.SetActive (true);
if (Input.GetButtonDown ("Action")) {
WeaponSelectQuestionText.SetActive (false);
Colt45Text.SetActive (true);
Glock17Text.SetActive (true);
ScarHText.SetActive (true);
SKSText.SetActive (true);
if (Input.GetButtonDown ("Weapon Wall No. 1")) {
Colt45.SetActive (true);
}
if (Input.GetButtonDown ("Weapon Wall No. 2")) {
Glock17.SetActive (true);
}
if (Input.GetButtonDown ("Weapon Wall No. 3")) {
ScarH.SetActive (true);
}
if (Input.GetButtonDown ("Weapon Wall No. 4")) {
SKS.SetActive (true);
}
Where and when is this code executed? GetButtonDown has to be executed every frame or it doesn't work. That's why your four nested GetButtonDown can't work as well. Even when the outer "if" would work( which depends on where this piece of code is located ) the nested "ifs" can't work at all.
Answer by Bunny83 · Oct 19, 2017 at 12:37 AM
You probably want to do something like this:
// when we enter the trigger we activate the text
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "Player")
{
WeaponSelectQuestionText.SetActive (true);
}
}
void OnTriggerStay (Collider other)
{
if (other.gameObject.name == "Player")
{
if (Input.GetButtonDown ("Action"))
{
WeaponSelectQuestionText.SetActive (false);
Colt45Text.SetActive (true);
Glock17Text.SetActive (true);
ScarHText.SetActive (true);
SKSText.SetActive (true);
}
}
}
Here we only activate the text once when the player enters the trigger. In OnTriggerStay we check if the "Action" button is pressed and then deactivate the text. Now it's not reactivated immediately since OnTriggerEnter is only called once when you enter the trigger. If the player leaves the trigger area and reenters it again, the text would be displayed again. If you don't want this you have to use a boolean variable to remember the state that you already have triggered it and disabled it.
Note that you should not call Update or any of the Unity callbacks manually.. This will just mess up everything as your if statement would be processed twice each frame and therefore the code would be executed two times. If you want those buttons to only work when you're inside the trigger you should move the code also into the OnTriggerStay method. However do not nest GetButtonDown conditions. You have to place them on the same level so they are executed every time.
Answer by connorwforman · Oct 18, 2017 at 10:49 PM
I tried this, and it still didn't work. void Update () {
if (Input.GetButtonDown ("Weapon Wall No. 1")) {
Colt45.SetActive (true);
}
if (Input.GetButtonDown ("Weapon Wall No. 2")) {
Glock17.SetActive (true);
}
if (Input.GetButtonDown ("Weapon Wall No. 3")) {
ScarH.SetActive (true);
}
if (Input.GetButtonDown ("Weapon Wall No. 4")) {
SKS.SetActive (true);
}
}
void OnTriggerStay (Collider other) {
if (other.gameObject.name == "Player") {
WeaponSelectQuestionText.SetActive (true);
if (Input.GetButtonDown ("Action")) {
WeaponSelectQuestionText.SetActive (false);
Colt45Text.SetActive (true);
Glock17Text.SetActive (true);
ScarHText.SetActive (true);
SKSText.SetActive (true);
Update ();
}
}
Of course this doesn't work. Your logic is wrong. Look OnTriggerStay is executed every frame while the player is inside that trigger. Each frame you set the "WeaponSelectQuestionText" active. and you check if the Action button got pressed to deactivate it. However when you actually press the button you enter the if statement with GetButtonDown once. There you actually deactivate the object. However remember what i just said? OnTriggerStay is executed every frame. That means the next frame after you pressed your button you activate the object again. So it will only be deactivated for a single frame.
Answer by lipemon1 · Oct 18, 2017 at 11:20 PM
First, go to Edit > Project Settings > Input.
Now, look to your Inspector and verify if there is an input with your button verification name "Action".
To do a quick test, change your verification to an existing button like Jump. Like the code below:
if (Input.GetButtonDown ("Jump")) { //DO YOUR CODE HERE }
Answer by OusedGames · Oct 18, 2017 at 11:57 PM
Hello buddy,
OnTriggerStay works just like Update, a continuous Method that will run every frame/second. You will want to put the code that checks for layer input in here.
But as my friend @lipemon1 saind, your problem must be in the InputManager, as this button you are using might not have been setup. Maybe you could use keycodes that are easier intead of buttons, and just boolean variables to check the weapon wall number.
Hope it works
No, it is not an input thing, when I press the action button that brings up my options, the question text doesn't hide.