This is exactly the script! By use them I mean I can drop the items even if I don’t have them picked!
Ah okay! Of course, I should have seen that… lol
Right at the start of your RemoveItem() method, simply check :
if(item == null) return;
Since you have a simple setup for a list here
That will suffice for now, I think.
Damn it worked! Thanks! I got one last problem… Right now I need to pick the items in order… Because if I pick item 1 and item 1 is on item slot 2 and if i try to spawn it, it’s spawning item 2… This is probably because of this script!
if (Input.GetKeyDown(KeyCode.Alpha1))
{
RemoveItem(items[0]);
}
else if (Input.GetKeyDown(KeyCode.Alpha2))
{
RemoveItem(items[1]);
}
else if (Input.GetKeyDown(KeyCode.Alpha3))
{
RemoveItem(items[2]);
}
else if (Input.GetKeyDown(KeyCode.Alpha4))
{
RemoveItem(items[3]);
}
Because I’m accessing the arrays directly with 0, 1, 2, 3
That’s getting a bit more complicated, but I kinda thought you’d ask about this eventually lol
I think I can explain a suggestion to you in words, and maybe you can see what I mean.
If you put the GameObject reference (for the item you want to pickup/drop) inside the Item class, then when you drop by the # you press, you find the Item at the index (#) and then instantiate the Item’s gameobject.
Does that make sense? This way the list can be a bit more dynamic. You could store Item 3 in slot 1, but when you click ‘1’ to drop, it will drop item 3. That’s kinda what you want, right?
Man you don’t know how much I appreciate your help!! It’s working perfectly! Thanks!!
Awesome.
I’m glad, man. Enjoy your game making!
Hi! I don’t want to make new post so I’m asking here because you know what I want to do! Everything is working perfectly thanks to your help but I’ve found a little bug. I can collect as many items as I want witch is not good. Can you help me again?
This is my pick up script -
private bool isTrigger;
public GameObject player;
public inventory inventory;
public Item item;
public Text text;
public GameObject panel;
void Start()
{
text = GameObject.Find("objPickText/GameObject/Panel/Text").GetComponent<Text>();
panel = GameObject.Find("objPickText/GameObject/Panel");
player = GameObject.Find("Player");
text.gameObject.SetActive(false);
panel.SetActive(false);
}
void Update()
{
if (inventory == null)
{
inventory = GameObject.FindObjectOfType<inventory>();
}
if (isTrigger == false)
{
gameObject.GetComponent<Rigidbody>().useGravity = true;
gameObject.GetComponent<Rigidbody>().isKinematic = false;
gameObject.GetComponent<BoxCollider>().isTrigger = false;
}
isTrigger = true;
}
void OnTriggerEnter(Collider co)
{
if (co.tag == "Player")
{
panel.SetActive(true);
text.gameObject.SetActive(true);
text.text = "Натисни бутонът Е, за да вземеш предмета!";
}
else
{
panel.SetActive(false);
text.gameObject.SetActive(false);
}
}
void OnTriggerStay(Collider co)
{
//from here I'm adding my items to the inventory
if (co.gameObject.name == "Player")
{
if (Input.GetKeyDown(KeyCode.E))
{
inventory.AddItem(item);
GameObject.DestroyObject(this.gameObject);
}
}
}
void OnTriggerExit(Collider co)
{
panel.SetActive(false);
text.gameObject.SetActive(false);
isTrigger = true;
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "terrain")
{
gameObject.GetComponent<Rigidbody>().useGravity = false;
gameObject.GetComponent<Rigidbody>().isKinematic = true;
gameObject.GetComponent<BoxCollider>().isTrigger = true;
}
else if (col.gameObject.name == null)
{
isTrigger = false;
}
}
I can try to help… If your AddItem method is still pretty much the same as in your previous posts, you could try to change the method signature to be “public bool AddItem(Item item)”
then, if it finds the spot, ie: items == null you add it, and return true
otherwise, if it finishes the loop and hasn’t found one return false
This way, in your OnTriggerStay function, when the player presses ‘E’ you can write:
csharp* *if (Input.GetKeyDown(KeyCode.E)) { if(inventory.AddItem(item)) GameObject.DestroyObject(this.gameObject); }* *
That is, if I understand your message correctly. You’re saying you can pick up as much as you want - meaning they disappear from the ground. This way, you’ll get true if you had room and destroy the item on the ground.
You’ll get false if there was no room, and therefore the item in the world will remain.
Yep! It’s working! Thanks again :)!
Great, great. You’re welcome.
Enjoy.