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.
Button = " + Input.GetButton (“Pickup”) + ". holdPickup = " + holdPickup + ". isholding = " + charInventory.isHoldingItem());
holdPickup = true;
charInventory.getHoldingItem ().GetComponent ().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;
}
}
}
}