So, what I’m trying to accomplish is have my player hold one object, and the other sit on a counter, then when the player cursor is hovering over the counter and E is pressed, the player “places” the held object down to combine with the other one, but if Shift E is pressed, the item in combined into his inventory (inventory size one), I’ve got the instantiation down, but not to where the two initial items are destroyed and the resulting item is fully replacing the respecting item depending on if shift is pressed, I’ve only worked on the pick up combine so far, not the place until I can get the first handled.
(This is the script on the player handling the inventory)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class Holding : MonoBehaviour{
public Transform Hold;
public LayerMask pick;
public GameObject itemHeld;
public GameObject Cursor;
public bool select;
public bool holding;
public Selection sel;
public GameObject box;
public Combining combine;
void Update(){
sel=box.GetComponent<Selection>();
if(Input.GetKeyDown(KeyCode.Space)){
if(itemHeld&&!sel.held&&select){
Drop();
}else{
Collider2D seen = Physics2D.OverlapCircle(Cursor.transform.position, .4f, pick);
Grab(seen);
}
}
}public void Grab(Collider2D uppie){
if(uppie&&holding==false&&uppie.transform.parent!=transform){
sel.held=null;
itemHeld = uppie.gameObject;
itemHeld.transform.position = Hold.position;
itemHeld.transform.parent = transform;
holding=true;
combine=itemHeld.GetComponent<Combining>();
combine.hold=transform.GetComponent<Holding>();
itemHeld.GetComponent<SpriteRenderer>().sortingOrder = 2;
if(itemHeld.GetComponent<Rigidbody2D>())
itemHeld.GetComponent<Rigidbody2D>().simulated = false;
}
}public void Drop(){
sel.held=itemHeld;
itemHeld.transform.position=Cursor.transform.position;
itemHeld.transform.parent = sel.transform;
holding=false;
itemHeld.GetComponent<SpriteRenderer>().sortingOrder = 0;
if(itemHeld.GetComponent<Rigidbody2D>())
itemHeld.GetComponent<Rigidbody2D>().simulated = true;
itemHeld = null;
combine.hold=null;
combine=null;
}
}
(This is the counter’s script)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Selection : MonoBehaviour{
public Holding hold;
public GameObject held;
public Combining combine;
public Interact act;
void Start(){
act=GetComponent<Interact>();
}private void OnTriggerEnter2D(Collider2D collider){
if(collider.tag==("Cursor")){
hold=collider.transform.parent.transform.parent.GetComponent<Holding>();
hold.box=this.gameObject;
hold.select=true;
act.selected=true;
}
}private void OnTriggerExit2D(Collider2D collider){
if(collider.tag==("Cursor")){
hold.select=false;
hold.box=null;
hold=null;
act.selected=false;
}
}void Update(){
if(held){
combine=held.GetComponent<Combining>();
combine.sel=transform.GetComponent<Selection>();
}else{
combine.sel=null;
combine=null;
}
if(hold&&combine&&hold.combine){
combine.second=hold.combine;
hold.combine.second=combine;
}else{
combine.second.second=null;
combine.second=null;
}
}
}
(This is the script placed on both combined objects)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class Combining : MonoBehaviour
{
public List<string> other;
public List<GameObject> final;
public int item;
public GameObject finalItem;
public Collider2D FinalCollider;
public Holding hold;
public Selection sel;
public Combining second;
void Update(){
if(other.Contains(second.gameObject.name)){
item=other.IndexOf(second.gameObject.name);
}
if(Input.GetKeyDown(KeyCode.E)){
if(Input.GetKey(KeyCode.LeftShift)){
GrabCombine();
}else{
PlaceCombine();
}
}
}public void GrabCombine(){
if(hold&&second){
Debug.Log(final[item].gameObject.name);
finalItem=Instantiate(final[item], transform.position, transform.rotation);
FinalCollider=finalItem.GetComponent<Collider2D>();
hold.Grab(FinalCollider);
//Destroy(second.gameObject);
//Destroy(this.gameObject);
}
}public void PlaceCombine(){
if(sel&&second){
}
}
}
Sorry if it’s too long, I’m kind of a beginner, but have some decent experience