Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

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

Discussion in 'Scripting' started by alpermimoglu, May 15, 2024.

  1. alpermimoglu

    alpermimoglu

    Joined:
    Nov 25, 2019
    Posts:
    2
    This is the script that I am trying to update the shape gameobject and the currentSelectedObject in runtime.

    Code (CSharp):
    1. public GameObject currentSelectedObject;
    2.  
    3.     public GameObject shape;
    4.  
    5.     public void RotateClockwise()
    6.     {
    7.         shape.transform.Rotate(0f, 0f, -10f);
    8.     }
    9.  
    10.     public void RotateAntiClockwise()
    11.     {
    12.         shape.transform.Rotate(0f, 0f, 10f);
    13.     }
    14.  
    15.     public void UpdateSelected(GameObject newSelection)
    16.     {
    17.  
    18.          if (currentSelectedObject == null)
    19.         {
    20.             if (shape.GetComponent<DragAndDrop>().IsDragging == true)
    21.             {
    22.                 currentSelectedObject = newSelection;
    23.                 newSelection = shape;
    24.             }
    25.         }
    and here is the dragging script mentioned that I use to drag the puzzle pieces

    Code (CSharp):
    1.  private RectTransform rectTransform;
    2.     public Image image;
    3.     public bool IsDragging;
    4.  
    5.  
    6.  
    7.     public void OnBeginDrag(PointerEventData eventData)
    8.     {
    9.         // throw new System.NotImplementedException();
    10.  
    11.         image.color = new Color32(255, 255, 255, 170);
    12.     }
    13.  
    14.     public void OnDrag(PointerEventData eventData)
    15.     {
    16.         //rectTransform.anchoredPosition += eventData.delta;
    17.  
    18.         transform.position = Input.mousePosition;
    19.         IsDragging = true;
    20.  
    21.     }
    22.  
    23.     public void OnEndDrag(PointerEventData eventData)
    24.     {
    25.         image.color = new Color32(255, 255, 255, 255);
    26.         IsDragging = false;
    27.     }
    28.  
    29.  
    30.  
    31.     // Start is called before the first frame update
    32.     void Start()
    33.     {
    34.  
    35.         rectTransform = GetComponent<RectTransform>();
    36.         image = GetComponent<Image>();
    37.      
    38.     }
    Many thanks to anyone that reads this.
     
    Last edited: May 15, 2024
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,445
    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/



    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.

    https://docs.unity3d.com/Manual/ManagedCodeDebugging.html

    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.
     
    alpermimoglu likes this.