The question is answered, right answer was accepted
Boolean sets itself to false when using layers.
I have a script that will pick up an item when pressing "Pickup" button which is K. If you're already holding an item it will drop it instead.
It was working perectly fine until I placed the item to pickup and the trigger that detects the items into other layers, so that the trigger can only collide with the items and items can't collide with the player collider.
Now whenever I try to pickup an item, it will pick it up then immediately drop it. It seems the holdPickup bool will reset to false when it shouldn't causing if (Input.GetButton ("Pickup") && !holdPickup && charInventory.isHoldingItem ()) { to be true, which will call the drop method from another script.
holdPickup is to check if the player has let go of the button and pressed it again so that this wont happen.
Even if I remove if (!Input.GetButton ("Pickup") && holdPickup) { holdPickup = false; } from the script so that there is nothing nowhere that sets holdPickup back to false, it will still be set back to false. There is nothing in any of my other scripts that sets holdPickup to false.
Any idea what would be causing this? I have'nt worked with layers before, what would make layers cause a bool to set back to false? It seems ilogical...
Thanks for any answer!
using UnityEngine;
using System.Collections;
public class CharPickUp : MonoBehaviour {
CharInventory charInventory;
bool holdPickup;
void Start() {
charInventory = transform.parent.GetComponent<CharInventory> ();
}
void Update() {
if (!Input.GetButton ("Pickup") && holdPickup) {
Debug.Log ("Let go of Pickup button");
holdPickup = false;
}
if (Input.GetButton ("Pickup") && !holdPickup && charInventory.isHoldingItem ()) { //calls on drop method.
Debug.Log ("Call on drop.\nButton = " + Input.GetButton ("Pickup") + ". holdPickup = " + holdPickup + ". isholding = " + charInventory.isHoldingItem());
holdPickup = true;
charInventory.getHoldingItem ().GetComponent<PickUpableItem> ().Dropped ();
charInventory.setHoldingItem (null);
}
}
void OnTriggerStay2D(Collider2D col) {
if (Input.GetButton ("Pickup") && !holdPickup && !charInventory.isHoldingItem ()) {
holdPickup = true;
Debug.Log ("Tried to pick up " + col.gameObject);
Debug.Log ("holdPickup = " + holdPickup);
switch (col.gameObject.tag) {
case "rock":
charInventory.setHoldingItem (col.gameObject);
col.gameObject.GetComponent<PickUpableItem> ().PickedUp (this.gameObject);
break;
case "branch":
charInventory.setHoldingItem (col.gameObject);
col.gameObject.GetComponent<PickUpableItem> ().PickedUp (this.gameObject);
break;
}
}
}
}
Answer by Spelix95 · Jul 12, 2016 at 04:30 PM
A few hours after posting this question, I randomly stumbled on the answer.
This script is dublicated from an older one that only handled both punching and picking up. We decided it was better to split the two of them up and so we did. The problem then appeared.
The thing was that we never removed the earlier script from the trigger, only applied the new ones. So each time we tried to pick something up, we had two seperate pick-up scripts running, one picked it up and held it, the other dropped it immidatly.
TL;DR: Don't dublicate scripts and forgetting to remove the old one