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. Dismiss Notice

Problem to add GameObject a one List

Discussion in 'Scripting' started by Xremix30, Nov 28, 2020.

  1. Xremix30

    Xremix30

    Joined:
    Jul 2, 2018
    Posts:
    6
    Hi,
    I want add objects when drag in second inventory a one list but i can't
    i have this code when item is dragged in second inventory but now the my list Add automaticly the childs that contain the objects but i want this happens only drag the object in place.

    Code (CSharp):
    1. [SerializeField] Transform slots;
    2.  
    3.     public List<GameObject> itemsList;
    4.     // Use this for initialization
    5.  
    6.     void Start () {
    7.         itemsList = new List<GameObject> (); // Inicializa a lista de Objectos
    8.  
    9.         HasChangedPlace ();
    10.     }
    11.        
    12.  
    13.     public void HasChangedPlace(){
    14.        
    15.  
    16.         foreach (Transform item in slots) {
    17.             if(!itemsList.Contains(item.gameObject)){
    18.                
    19.             itemsList.Add (item.gameObject);
    20.  
    21.  
    22.                 Debug.Log (item.gameObject);
    23.  
    24.  
    25.             }
    26.                
    27.         }
    28.     }
    29. }
     
  2. VishwasGagrani

    VishwasGagrani

    Joined:
    May 12, 2018
    Posts:
    81
    This code does nothing to drag and drop. You must use a script on object that has got a method OnMouseDrag
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDrag.html


    Code (CSharp):
    1.  
    2. public class ObjectToDrag : MonoBehaviour
    3. {
    4.  
    5.     float zPos_Flt ;
    6.  
    7.     void Start()
    8.     {
    9.        ..
    10.     }
    11.     void OnMouseDrag ( ) //Press mouse button on the object, and then move without releasing
    12.     {
    13. ....
    14.  
    15.     }
    16.  
    17. }