- Home /
code not working
I cant get this code to execute but it works fine without the key press any way to fix this
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player : MonoBehaviour { public InventoryObject inventory;
public void OnTriggerStay(Collider other)
{
var item = other.GetComponent<Item>();
if(item && Input.GetKeyDown(KeyCode.E))
{
inventory.AddItem(item.item, 1);
Destroy(other.gameObject);
}
}
}
In order to help us answer your question, you should include what you expect to happen when you run this code, as well as what actually happens when you run the code (including any specific errors).
As a general statement, you shouldn't be putting something that isn't a bool
into the conditions of an if statement. if(item)
is not immediately clear what you are trying to do.
Answer by Jon_Olive · Jul 04, 2020 at 12:47 AM
@agentX900 Change to
if(item != null && Input.GetKeyDown(KeyCode.E))
it's unclear what just testing if(item) will return - and I suspect for that reason the && operator isn't working.
Your answer
Follow this Question
Related Questions
Why The player don't move? 0 Answers
BCE0019: 'text' is not a member of UnityEngine.component 2 Answers
i need help with make an attack animation only when the player is near 1 Answer
How can I make the character move while jumping 1 Answer
Simple - Visual Studio Code not loading script when icon is clicked through unity *Video provided* 0 Answers