I have a pickup item script but how do i make it so it only picks up when i press “E”?
heres the script:
using UnityEngine;
public class Pickup : MonoBehaviour
{
private Inventory inventory; //The inventory
public GameObject itemButton; //The ui icon for the item
public GameObject icon; //The icon in game
public GameObject itemInGame; //The actual item
public Transform itemPosition; //The position where the item should go
public Transform itemHolder; //The item holder
// Start is called before the first frame update
void Start()
{
inventory = GameObject.FindGameObjectWithTag("Inventory").GetComponent<Inventory>();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.CompareTag("Player")) //If the player collides with the icon hitbox
{
for (int i = 0; i < inventory.slots.Length; i++) //Run a for loop for every inventory slot
{
if(inventory.isFull[i] == false) //If the slot isn't full
{
//Item can be picked up and added to inventory.
inventory.isFull[i] = true; //Set the slot to be full
Instantiate(itemButton, inventory.slots[i].transform, false); //Instantiate the ui icon
icon.SetActive(false); //Turn off the icon
itemInGame.SetActive(true); //And replace it with the actual gun.
transform.position = itemPosition.position; //Set the item position to the correct position on the player
transform.SetParent(itemHolder); //Make it a child of the player
transform.localRotation = Quaternion.identity; //Make the rotation 0, 0, 0
break; //Stop running the for loop.
}
}
}
}
}
No such thing as too many comments if it helps you.
You need to check for input in Update. So with OnTriggerEnter2D you simply store whatever item you come within range. Then when they hit a key, in Update you can trigger the code to pick it up. If they Exit the trigger without picking it up, you can unregister the stored “pickup target”.
Again, in Update, check for input to drop an item also.
using UnityEngine;
public class Pickup : MonoBehaviour
{
private Inventory inventory; //The inventory
public GameObject itemButton; //The ui icon for the item
public GameObject icon; //The icon in game
public GameObject itemInGame; //The actual item
public Transform itemPosition; //The position where the item should go
public Transform itemHolder; //The item holder
private bool isReadyTopickUp = false;
// Start is called before the first frame update
void Start()
{
inventory = GameObject.FindGameObjectWithTag("Inventory").GetComponent<Inventory>();
}
private void Update()
{
if(Input.GetKeyDown(KayCode.E) && isReadyTopickUp)
{
ProcessPickUp();
isReadyTopickUp = false;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.CompareTag("Player"))
{
isReadyTopickUp = true;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if(collision.CompareTag("Player"))
{
isReadyTopickUp = false;
}
}
private void ProcessPickUp()
{
if(collision.CompareTag("Player")) //If the player collides with the icon hitbox
{
for (int i = 0; i < inventory.slots.Length; i++) //Run a for loop for every inventory slot
{
if(inventory.isFull[i] == false) //If the slot isn't full
{
//Item can be picked up and added to inventory.
inventory.isFull[i] = true; //Set the slot to be full
Instantiate(itemButton, inventory.slots[i].transform, false); //Instantiate the ui icon
icon.SetActive(false); //Turn off the icon
itemInGame.SetActive(true); //And replace it with the actual gun.
transform.position = itemPosition.position; //Set the item position to the correct position on the player
transform.SetParent(itemHolder); //Make it a child of the player
transform.localRotation = Quaternion.identity; //Make the rotation 0, 0, 0
break; //Stop running the for loop.
}
}
}
}
}
Edit Error:
private void ProcessPickUp()
{
for (int i = 0; i < inventory.slots.Length; i++) //Run a for loop for every inventory slot
{
if(inventory.isFull[i] == false) //If the slot isn't full
{
//Item can be picked up and added to inventory.
inventory.isFull[i] = true; //Set the slot to be full
Instantiate(itemButton, inventory.slots[i].transform, false); //Instantiate the ui icon
icon.SetActive(false); //Turn off the icon
itemInGame.SetActive(true); //And replace it with the actual gun.
transform.position = itemPosition.position; //Set the item position to the correct position on the player
transform.SetParent(itemHolder); //Make it a child of the player
transform.localRotation = Quaternion.identity; //Make the rotation 0, 0, 0
break; //Stop running the for loop.
}
}
}
i added it, it says that the i++ on line 51 is unreachable, help!
using UnityEngine;
public class Pickup : MonoBehaviour
{
private Inventory inventory; //The inventory
public GameObject itemButton; //The ui icon for the item
public GameObject icon; //The icon in game
public GameObject itemInGame; //The actual item
public Transform itemPosition; //The position where the item should go
public Transform itemHolder; //The item holder
private bool isReadyToPickUp = false;
// Start is called before the first frame update
void Start()
{
inventory = GameObject.FindGameObjectWithTag("Inventory").GetComponent<Inventory>();
}
private void Update()
{
if(Input.GetKeyDown(KeyCode.E) && isReadyToPickUp)
{
ProcessPickUp();
isReadyToPickUp = false;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
isReadyToPickUp = true;
Debug.Log("Ready to pick up.");
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
isReadyToPickUp = false;
Debug.Log("Exited hitbox");
}
}
private void ProcessPickUp()
{
for (int i = 0; i < inventory.slots.Length; i++) //Run a for loop for every inventory slot
{
//Item can be picked up and added to inventory.
inventory.isFull[i] = true; //Set the slot to be full
Instantiate(itemButton, inventory.slots[i].transform, false); //Instantiate the ui icon
icon.SetActive(false); //Turn off the icon
itemInGame.SetActive(true); //And replace it with the actual gun.
transform.position = itemPosition.position; //Set the item position to the correct position on the player
transform.SetParent(itemHolder); //Make it a child of the player
transform.localRotation = Quaternion.identity; //Make the rotation 0, 0, 0
break; //Stop running the for loop.
}
}
}
You have a break in your for loop with no conditions, so it can’t ever loop. It runs once and ends. Also, make sure you’re reviewing your code to see for these logic errors.
i havent use break; inside a loop before, i still think about it if break messing the loop up. have you try without the break; will your pickup code still working?