- Home /
IF statement not working?
Hey everybody. Im trying to make a pick-up-and-drop script. So far everything works great, except for one thing. I'm trying to limit the items you can pick up to a single item at a time. So the player is only able to pick one item, and then having to drop the item before picking up a new one. Only problem is that the IF statement doesn't give a crap about the rules it was suppose to follow ? ? ?
public class grab : MonoBehaviour {
public Transform playerH;
public bool handsfull = false;
public bool guigrab = false;
public bool cangrab = false;
private int buttonWidth = 200;
private int buttonHeight = 50;
public void OnTriggerEnter (Collider collision)
{
if (collision.gameObject.tag == "Player")
{
cangrab = true;
}
}
public void OnTriggerExit (Collider collision)
{
if (collision.gameObject.tag == "Player")
{
cangrab = false;
guigrab = false;
}
}
void OnGUI()
{
if(guigrab)
{
GUI.Box (new Rect (0, 0, buttonWidth, buttonHeight), "Right click to pick up object");
}
}
void FixedUpdate () {
if (cangrab == true)
{
guigrab = true;
if(Input.GetKey(KeyCode.Mouse1) && handsfull != true)
{
this.transform.parent = playerH;
this.transform.localPosition = new Vector3(0,0,0);
this.rigidbody.isKinematic = true;
guigrab = false;
handsfull=true;
}
if (handsfull == true && Input.GetKey (KeyCode.Mouse1) && Input.GetKey(KeyCode.Mouse0))
{
handsfull = true;
this.transform.parent = null;
this.rigidbody.isKinematic = false;
}
}
}
}
Im greatfull for any kind of help !
couple of things from looking at the code:
cangrab
& guigrab
seem to be the same, why not use the same variable?
this line:
if (handsfull == true && Input.Get$$anonymous$$ey ($$anonymous$$eyCode.$$anonymous$$ouse1) && Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$ouse0))
should probably be else if
... because you're setting handsfull = true
in the previous if
, and then possibly dropping the item.
also, following that, why are you setting handsfull = true
when you know it's always true
?
void FixedUpdate () {
if (cangrab == true)
{
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$ouse1) && handsfull == false)
{
this.transform.parent = playerH;
this.transform.localPosition = new Vector3(0,0,0);
this.rigidbody.is$$anonymous$$inematic = true;
guigrab = false;
handsfull = true;
}
else if (handsfull == true && Input.Get$$anonymous$$ey ($$anonymous$$eyCode.$$anonymous$$ouse1) && Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$ouse0))
{
handsfull = false;
this.transform.parent = null;
this.rigidbody.is$$anonymous$$inematic = false;
}
}
}
You are right about the two variables.. Fool I am.. The handsfull was suppose to be False like this. The problem is that when handsfull is equal to True, you arn't suppose to pick up a new item. But somehow the IF-statement don't care about anything ?
It takes practice to do this stuff, especially with ANDs and elses. Realize than in about two months you'll delete this and make a better version from scratch.
If you absolutely have to insult your if statements, suggest that their else's are cheating on them.
Using $$anonymous$$et$$anonymous$$ey ins$$anonymous$$d of Get$$anonymous$$eyDown may have left you with an oscillator.
Your answer
Follow this Question
Related Questions
if Statement Help Increment Value 2 Answers
(if statement) is acting weird 1 Answer
TBG,If,While 1 Answer
How to determine if your List has more than 1 of the same object listed? 1 Answer
Check if gravity is inverted 1 Answer