- Home /
How to Find The Last Item To Use A Specific Script
Do you know if you can help me with my problem. I want to find the last item that used my itempickup script then instantiate it, This is a drop item system for my stickman game. The problem is if lets say i pickup a wood sword, all is great, but then when i go to pickup a iron sword, the iron sword is picked up but instead of instantiating the wood sword to the players position, it instantiates the iron sword. Thanks In Advance! So Heres the Code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI;
public class Itempickup : MonoBehaviour { using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI;
public class Itempickup : MonoBehaviour {
public GameObject Item;
bool playerContact;
public Transform Feet;
public bool equippedOn;
public GameObject ThisItem;
public LayerMask Weapons;
public GameObject LastItem;
[SerializeField]
private Text text;
public void Start()
{
text.gameObject.SetActive(false);
}
private void Update() { if (playerContact == true && Input.GetKeyDown(KeyCode.E)) {
GameObject[] ThisItem = GameObject.FindGameObjectsWithTag ("Weapons");
foreach(GameObject go in ThisItem)
{
go.SetActive(false);
Spawn();
}
Equip();
}
}
void Equip() {
Item.SetActive(true);
//ThisItem.SetActive(false);
Destroy(gameObject);
equippedOn = true;
}
void Spawn()
{
Instantiate(ThisItem, transform.position, Quaternion.identity);
Debug.Log("Prefab Made" + ThisItem.name);
}
void Drop() {
Item.SetActive(false);
ThisItem.SetActive(true);
equippedOn = false;
}
void OnTriggerEnter2D(Collider2D collision)
{
if(collision.CompareTag("Pickup"))
{
playerContact = true;
text.gameObject.SetActive(true);
}
}
void OnTriggerExit2D(Collider2D collision)
{
if (collision.CompareTag("Pickup"))
playerContact = false;
text.gameObject.SetActive(false);
}
}
If you want our help, can you atleast put in "some" effort into writing and rereading your questions that you post, you can clearly see your code is not formatted correctly. We will not put our effort in trying to help you, if you don't atleast put some effort into properly asking a question.
@ShadyProductions ok sure but the thing is that unity seperates the code really badly for no reason when all should be good so sorry if its hard to read im a beginner and i dont know why unity seperates so much. i redid the question above. Basically i want to instantiate the last item i picked up instead of the current one i picked up.
Your answer
