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

Bug puzzle like game

Discussion in 'Scripting' started by MUMUchacho, Jun 23, 2023.

  1. MUMUchacho

    MUMUchacho

    Joined:
    Jun 23, 2023
    Posts:
    2
    Hey, i've got a project and i have some issues.

    View attachment 1261081

    I am supposed to be able to select an image, to drag it and rotate it if i want but i cant even select an image even thought ther's no compile errors

    1st code:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class LinkingScripts2 : MonoBehaviour
    5. {
    6.     public bool isUniqueMode = true;
    7.     private Transform selectedObject; // Stores the transform of the last selected object
    8.  
    9.     private void Update()
    10.     {
    11.         // Check for mode switching input
    12.         if (Input.GetKeyDown(KeyCode.C))
    13.         {
    14.             isUniqueMode = true;
    15.         }
    16.         else if (Input.GetKeyDown(KeyCode.V))
    17.         {
    18.             isUniqueMode = false;
    19.         }
    20.  
    21.         if (isUniqueMode)
    22.         {
    23.             // Move and rotate selected object
    24.             if (selectedObject != null)
    25.             {
    26.                 // Move object with left mouse click
    27.                 if (Input.GetMouseButton(0))
    28.                 {
    29.                     Vector3 moveDirection = Camera.main.ScreenToWorldPoint(Input.mousePosition) - selectedObject.position;
    30.                     moveDirection.z = 0f;
    31.                     selectedObject.Translate(moveDirection * Time.deltaTime);
    32.                 }
    33.  
    34.                 // Rotate object with mouse wheel
    35.                 float rotationAmount = Input.GetAxis("Mouse ScrollWheel") * 50f;
    36.                 selectedObject.RotateAround(selectedObject.position, Vector3.forward, rotationAmount);
    37.             }
    38.         }
    39.         else
    40.         {
    41.             // Move camera with left mouse click
    42.             if (Input.GetMouseButton(0))
    43.             {
    44.                 float moveHorizontal = Input.GetAxis("Mouse X");
    45.                 float moveVertical = Input.GetAxis("Mouse Y");
    46.                 transform.Translate(new Vector3(moveHorizontal, moveVertical, 0));
    47.             }
    48.  
    49.             // Zoom in/out with mouse wheel
    50.             float zoomFactor = 1.0f + Input.GetAxis("Mouse ScrollWheel") * 0.1f;
    51.             transform.localScale *= zoomFactor;
    52.         }
    53.     }
    54.  
    55.     private void OnMouseDown()
    56.     {
    57.         if (isUniqueMode)
    58.         {
    59.             // Select the object clicked on
    60.             RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    61.             if (hit.collider != null && hit.collider.CompareTag("Image"))
    62.             {
    63.                 selectedObject = hit.collider.transform;
    64.                 Debug.Log("Selected Image: " + selectedObject.name);
    65.             }
    66.             else
    67.             {
    68.                 selectedObject = null;
    69.             }
    70.         }
    71.     }
    72. }
    73.  
    74.  
    2nd code
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class ImageContainer2 : MonoBehaviour
    5. {
    6.     public float rotationSpeed = 50f;
    7.     public float moveSpeed = 0.1f;
    8.  
    9.     private bool dragging = false;
    10.     private int clickedImage = -1;
    11.     private Vector3 startDragPos;
    12.     private float offset_x;
    13.     private float offset_y;
    14.     private LinkingScripts2 linkingScripts2;
    15.  
    16.     public Rect[] imageRectList; // Define the imageRectList variable
    17.     public Rect sliderRect; // Define the sliderRect variable
    18.  
    19.     public Texture2D[] image_list; // Define the image_list variable
    20.     public float[] angleList; // Define the angleList variable
    21.  
    22.     private void Start()
    23.     {
    24.         linkingScripts2 = FindObjectOfType<LinkingScripts2>();
    25.     }
    26.  
    27.     private void Update()
    28.     {
    29.         if (linkingScripts2.isUniqueMode && !sliderRect.Contains(Input.mousePosition))
    30.         {
    31.             if (Input.GetMouseButtonDown(0))
    32.             {
    33.                 if (Input.GetMouseButtonDown(0))
    34.                 {
    35.                     for (int i = 0; i < imageRectList.Length; i++)
    36.                     {
    37.                         if (imageRectList.Contains(Input.mousePosition) && image_list.GetPixel((int)(Input.mousePosition.x - imageRectList.x), (int)(Input.mousePosition.y - imageRectList.y)).a > 0)
    38.                         {
    39.                             dragging = true;
    40.                             clickedImage = i;
    41.                             offset_x = imageRectList.x - Input.mousePosition.x;
    42.                             offset_y = imageRectList.y - Input.mousePosition.y;
    43.                         }
    44.                     }
    45.                 }
    46.             }
    47.             else if (Input.GetMouseButtonUp(0))
    48.             {
    49.                 dragging = false;
    50.             }
    51.             else if (Input.GetMouseButton(0) && dragging)
    52.             {
    53.                 float dx = Input.mousePosition.x - startDragPos.x;
    54.                 float dy = Input.mousePosition.y - startDragPos.y;
    55.                 for (int i = 0; i < imageRectList.Length; i++)
    56.                 {
    57.                     imageRectList.x += dx;
    58.                     imageRectList.y += dy;
    59.                 }
    60.                 startDragPos = Input.mousePosition;
    61.             }
    62.             else if (Input.mouseScrollDelta.y > 0 && clickedImage != -1)
    63.             {
    64.                 angleList[clickedImage] += 1.5f;
    65.             }
    66.             else if (Input.mouseScrollDelta.y < 0 && clickedImage != -1)
    67.             {
    68.                 angleList[clickedImage] -= 1.5f;
    69.             }
    70.         }
    71.     }
    72. }
    73.  

    3rd code (optionnal i think)

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class ImageSelection : MonoBehaviour
    5. {
    6.     private void Update()
    7.     {
    8.         if (Input.GetMouseButtonDown(0))
    9.         {
    10.             RaycastHit hit;
    11.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    12.  
    13.             if (Physics.Raycast(ray, out hit))
    14.             {
    15.                 GameObject selectedImage = hit.collider.gameObject;
    16.                 Debug.Log("Selected Image: " + selectedImage.name);
    17.             }
    18.         }
    19.     }
    20. }
    21.  
    22.  
    View attachment 1261102
     
    Last edited: Jun 23, 2023
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Please edit your post to use code-tags rather than plain-text for code.

    Thanks.
     
  3. MUMUchacho

    MUMUchacho

    Joined:
    Jun 23, 2023
    Posts:
    2
    It's better visually, thanks.
     
    MelvMay likes this.
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    You've tagged this with 2D physics but you're using 3D physics queries. If you don't know this then I'd say you should first do some 2D physics tutorials to help you get started.

    If it's 2D physics then you should know that you cannot perform a raycast into the screen along the Z axis, it's 2D physics, it only exists in 2D (XY). You can check if a point overlaps a collider with Physic2D.OverlapPoint.

    I've not looked at the rest of your scripts though.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Sounds like you have a bug... remember in Unity that how you set up your scene is just as important as the code itself, if not moreso.

    Time for you to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

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

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.