Cant convert GameObject to Draggable

I am making a building system similar to Bad Piggies and everything works, but when Instantiating a new object, for it to snap it needs to be added to a list (Shown Below) I tried to use List.Add() but that doesn’t work. I’m kind of new to unity and this is just making my brain hurt trying to figure it out.

207094-capture9.png

GridLock.cs

public class GridLock : MonoBehaviour
{

    public List<Transform> snapPoints;
    public List<Draggable> draggableObjects;
    public float snapRange = 0.5f;
    void Start()
    {
    foreach(Draggable draggable in draggableObjects)
        {
            draggable.dragEndedCallback = OnDragEnded;
        }
    }

    private void OnDragEnded(Draggable draggable)
    {
        float closestDistance = -1;
        Transform closestSnapPoint= null;

        foreach(Transform snapPoint in snapPoints)
        {
            float currentDistance = Vector2.Distance(draggable.transform.localPosition, snapPoint.localPosition);
            if (closestSnapPoint == null || currentDistance < closestDistance)
            {
            closestSnapPoint= snapPoint;
            closestDistance = currentDistance;
            }
        }
    
        if (closestSnapPoint != null && closestDistance <= snapRange) 
        { 
        draggable.transform.localPosition= closestSnapPoint.localPosition;
        }
    }
   
}

Draggable.cs

public class Draggable : MonoBehaviour
{
    public Camera Cam;
   public delegate void DragEndedDelegate(Draggable draggableObject);

    public DragEndedDelegate dragEndedCallback;

    private bool isDragged = false;
    private Vector3 mouseDragStartPosition;
    private Vector3 spriteDragStartPosition;

    

    private void Start()
    {
        
    }
    private void OnMouseDown()
    {
        isDragged = true;
        mouseDragStartPosition = Cam.ScreenToWorldPoint(Input.mousePosition);
        spriteDragStartPosition = transform.localPosition;
    }

    private void OnMouseDrag() 
    { 
    if(isDragged)
        {
            transform.localPosition = spriteDragStartPosition + (Cam.ScreenToWorldPoint(Input.mousePosition) - mouseDragStartPosition);
            if (Input.GetKeyDown(KeyCode.R)) 
            {
                gameObject.transform.Rotate(0, 0, 90);
            }
            if(Input.GetKeyDown(KeyCode.E))
            {
                Destroy(gameObject);
            }
        }
   }

    private void OnMouseUp()
    {
        isDragged = false;
       dragEndedCallback(this);
    }
}

CreationCode.cs

public class CreationCode : MonoBehaviour
{
    public GameObject Build;
    GridLock GL;

    void Start()
    {
        GL = GameObject.Find("Snapz").GetComponent<GridLock>();
    }

    private void OnMouseDown()
    {
        Vector2 BuildPos = Build.transform.position;
        GameObject A = Instantiate(Build, BuildPos, Quaternion.identity);
        
        GL.draggableObjects.Add(A);  //Doesnt work
       
        Destroy(GetComponent<CreationCode>());                       
    }
}

@dhill1354 - It seems like you’re trying to add a GameObject to a list of Draggable objects, which is causing the issue. You need to get the Draggable component from the instantiated GameObject before adding it to the list. Modify the CreationCode.cs script like this:

public class CreationCode : MonoBehaviour
{
    public GameObject Build;
    GridLock GL;

    void Start()
    {
        GL = GameObject.Find("Snapz").GetComponent<GridLock>();
    }

    private void OnMouseDown()
    {
        Vector2 BuildPos = Build.transform.position;
        GameObject A = Instantiate(Build, BuildPos, Quaternion.identity);
        
        // Get the Draggable component from the instantiated GameObject
        Draggable draggableComponent = A.GetComponent<Draggable>();
        
        // Add the Draggable component to the list
        GL.draggableObjects.Add(draggableComponent);

        Destroy(GetComponent<CreationCode>());
    }
}

Now, the instantiated GameObject’s Draggable component is added to the list, and it should work as expected.

Let me know if this gets you closer to an answer.

Exactly what SteenPetersen said… it’s because your instantiated object is known as a “GameObject” while the elements in your list are known as “Draggable”. All you need to do is add your instantiated object as a Draggable.

Here’s a way to do it in shorter lines:

Draggable A = Instantiate(Build, BuildPos, Quaternion.identity) as Draggable;

Then you can add it now:

GL.draggableObjects.Add(A);