- Home /
My script for a "shield" function goes on/off constantly when it's supposed to be on
Couldn't think of a great title for this question, heh. I did see a similar question before posting this, but didn't really get an answer out of it: Here
The problem I'm having is this… I want to hold a button and have a few things happen. Mostly a boolean should become false and a few things should become true as you can see below. The problem is, apparently with the way I'm telling it to "stop" it all just does't work properly. Everything flickers true/false constantly while holding the button instead of it all staying "on" basically. Apologies if none of this makes sense, but I can't think of a better way to put it. If someone could help me out a bit with this I would really appreciate it. I think it has something to do with the way I'm telling it all to stop.
Thanks. :)
Here's the part of the script that tells it to do this and which isn't working.
if(CanGetHurt == true && Input.GetButton("Shield"))
{
CanGetHurt = false;
ShieldParticles.enableEmission = true;
ShieldCollider.gameObject.active = true;
PlayerCollider.gameObject.collider.enabled = false;
} else
{
ShieldParticles.enableEmission = false;
ShieldCollider.gameObject.active = false;
CanGetHurt = true;
PlayerCollider.gameObject.collider.enabled = true;
}
Answer by GuyTidhar · Jun 04, 2012 at 05:39 AM
Would this help:
if ( Input.GetButtonDown("Shield") )
{
CanGetHurt = false;
ShieldParticles.enableEmission = true;
ShieldCollider.gameObject.active = true;
PlayerCollider.gameObject.collider.enabled = false;
}
if ( Input.GetButtonUp("Shield") )
{
ShieldParticles.enableEmission = false;
ShieldCollider.gameObject.active = false;
CanGetHurt = true;
PlayerCollider.gameObject.collider.enabled = true;
}
Ah, yes, that works. I had tried this before but it hadn't worked before due to another bug I had in the actual Unity editor. Thanks. :P
(Although you did take out an important part in the first if statement, but putting it back in it still works. :)
Glad it helped eventually :)
I hate it when the right fix gets lost when another bug clouds the one you're looking for :(
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Script error? BCE0077 0 Answers
(C#) A better way to limit actions to once per button press? 2 Answers
AddTorque doesn't rotate? 3 Answers
Basic movement in a grid 2 Answers