Trying to store an object reference dynamically during runtime to use buttons to rotate it.

This is the script that I am trying to update the shape gameobject and the currentSelectedObject in runtime.

public GameObject currentSelectedObject;

    public GameObject shape;
 
    public void RotateClockwise()
    {
        shape.transform.Rotate(0f, 0f, -10f);
    }

    public void RotateAntiClockwise()
    {
        shape.transform.Rotate(0f, 0f, 10f);
    }

    public void UpdateSelected(GameObject newSelection)
    {

         if (currentSelectedObject == null)
        {
            if (shape.GetComponent<DragAndDrop>().IsDragging == true)
            {
                currentSelectedObject = newSelection;
                newSelection = shape;
            }
        }

and here is the dragging script mentioned that I use to drag the puzzle pieces

 private RectTransform rectTransform;
    public Image image;
    public bool IsDragging;
 


    public void OnBeginDrag(PointerEventData eventData)
    {
        // throw new System.NotImplementedException();

        image.color = new Color32(255, 255, 255, 170);
    }

    public void OnDrag(PointerEventData eventData)
    {
        //rectTransform.anchoredPosition += eventData.delta;

        transform.position = Input.mousePosition;
        IsDragging = true;

    }

    public void OnEndDrag(PointerEventData eventData)
    {
        image.color = new Color32(255, 255, 255, 255);
        IsDragging = false;
    }

 

    // Start is called before the first frame update
    void Start()
    {

        rectTransform = GetComponent<RectTransform>();
        image = GetComponent<Image>();
     
    }

Many thanks to anyone that reads this.

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

Sounds like it is time for you to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

1 Like