Search Unity

Drag, drop items from inventory and replace a specific object with inventory item present in the gam

Discussion in 'Scripting' started by ganesh_be, Mar 26, 2020.

  1. ganesh_be

    ganesh_be

    Joined:
    Mar 10, 2020
    Posts:
    16
    1)When I drag and drop the object from inventory on the second object, the second object is replaced by the object present in inventory

    2) But When I drag the object from inventory and place the object at some other position the object moves to original position from where I picked but I want to keep in the inventory only



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Photo : InventoryItemBase
    6. {
    7.      public GameObject replace;
    8.  
    9.      public Inventory inventory;
    10.  
    11.      public HUD Hud;
    12.      // Start is called before the first frame update
    13.      public override string Name
    14.      {
    15.          get
    16.          {
    17.              return "Photo";
    18.          }
    19.      }
    20.  
    21.      public override void OnDrop()
    22.      {
    23.          RaycastHit hit = new RaycastHit();
    24.          Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    25.          replace = GameObject.FindGameObjectWithTag("replace");
    26.          if (Physics.Raycast(ray, out hit, 1000))
    27.          {
    28.              //TODO item drop functionality on a fixed wall
    29.          
    30.              if (hit.collider.gameObject.CompareTag("replace"))
    31.              {
    32.                  gameObject.SetActive(true);
    33.                  gameObject.transform.position = replace.gameObject.transform.position;
    34.                  gameObject.transform.rotation = replace.gameObject.transform.rotation;
    35.                  Destroy(replace.gameObject);
    36.              
    37.              }
    38.              else
    39.              {
    40.                 // Note sure
    41.              }
    42.        
    43.          }
    44.      }
    45. }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You're not being clear about what your actual problem is.
    Is that what you wanted, or are you describing this as a problem? I have no idea.
    You were just talking about 2 different objects in "1)", so which "the object" are you talking about now? Where is this "some other position", in the game world or in the inventory? You've shown your item's OnDrop code, but it sounds like you wanted to just move it around inside your inventory. If that is the problem, why not show that code?

    Also, you've shown 2 screen shots, but it doesn't look like there is anything in the inventory in either of them.
     
  3. ganesh_be

    ganesh_be

    Joined:
    Mar 10, 2020
    Posts:
    16
    Thank you for your reply @Joe-Censored

    1) I have two objects in the plane

    1.PNG

    2) I am picking up my first object and adding it to inventory

    2.PNG

    3) I am dragging the object from inventory by using the mouse pointer and dropping it on the second object and the second object is replaced by the object dragged from the inventory.

    3.PNG

    4) The problem lies here when I am dragging the object from inventory and placing it somewhere else the first object moves to the original position from where I picked.

    I want the second object to be replaced with the first one when I am dragging and dropping the first object on the second object ( which is working for me) but the first object should remain in the inventory rather than moving to the original position if I drag and drop the object at some other place (which the issue for me)

    4.PNG
     

    Attached Files:

    • 3.PNG
      3.PNG
      File size:
      110.6 KB
      Views:
      361
    Last edited: Mar 27, 2020