Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Drag, Drop and replace a specific Object

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

  1. ganesh_be

    ganesh_be

    Joined:
    Mar 10, 2020
    Posts:
    16
    1) I have two objects in the plane



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



    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.



    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)



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

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
  3. ganesh_be

    ganesh_be

    Joined:
    Mar 10, 2020
    Posts:
    16
    Thank you for your reply. No, I want the first object should move back to inventory if it doesn't hit the second object.
     
  4. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    Then call your Pickup() method in the else?
     
  5. ganesh_be

    ganesh_be

    Joined:
    Mar 10, 2020
    Posts:
    16
    I am using these specific lines of code, I am getting the result as expected but facing an error.

    NullReferenceException: Object reference not set to an instance of an object
    Photo_OnDrop () (at Assets/Scripts/Photo.cs:44)
    Inventory.RemoveItem (IInventoryItem item) (at Assets/Scripts/Inventory.cs:44)
    ItemDropHandler.OnDrop (UnityEngine.EventSystems.PointerEventData eventData) (at Assets/Scripts/ItemDropHandler.cs:27)
    UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IDropHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:85)
    UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
    UnityEngine.EventSystems.EventSystem:Update()

    Code (CSharp):
    1. else
    2.             {
    3.                 gameObject.SetActive(false);
    4.                 IInventoryItem _item = gameObject.GetComponent<IInventoryItem>();
    5.                 inventory.AddItem(_item);
    6.                 _item.OnPickup();
    7.                 Hud.CloseMessagePanel();
    8.             }