Search Unity

Question Papers, please like object superposition

Discussion in 'Scripting' started by Dontmakemecodeplease, May 23, 2023.

  1. Dontmakemecodeplease

    Dontmakemecodeplease

    Joined:
    May 23, 2023
    Posts:
    4
    Hi,
    So the problem is simple, In my 2D scene I have those 2 objects with a click and drag script attached to them. The game is heavily inspired by the papers, please UI, and so I want the same effect of when an object is clicked (to be dragged) it should be in front of all the objects in the scene and then when another object is clicked it should be put in front etc

    I have a script that does that (written by chatGPT because I don't have time to learn to code right now, and to be clear I'm a total newbie and trying to understand what he writes is great for learning, for me at least) BUT the script he wrote checks for the highest sorting order value (not sorting layer) and adds 1 to this value every time the object is clicked : But if I click one object multiple times its sorting order value increases by 1 every time it's clicked like for example I click 23 times on an object then its sorting order value would be 23 and then if I click on another object in the scene the object will automaticly take the highest sorting order and add 1 so it will be 24 juste in one click.

    The main idea of the game is to move objects around and I'm worried that after a long time the script wouldn't work anymore because the sorting order value would get too high.

    My questions are :

    Is it a real problem or maybe I shouldn't care about it? Because technically this works ...

    How could I fix this, or maybe there is a better way to do this? I tried setting back the other objects sorting order value to 0 when one is clicked (so that the highest stays 1 and lowest 0, but then if I had say 5 objects, the four objects under wouldn't keep the order you placed them in and instead they would be reset to where they were when the scene started.

    Here is the part of the script that maintain the object in front :

    Code (CSharp):
    1.  private void OnMouseDown()
    2.     {
    3.         // Set the sorting order of the object to the highest sorting order of all objects in the scene plus one
    4.         int highestSortingOrder = 0;
    5.         foreach (var spriteRenderer in FindObjectsOfType<SpriteRenderer>())
    6.         {
    7.             if (spriteRenderer.sortingOrder > highestSortingOrder)
    8.             {
    9.                 highestSortingOrder = spriteRenderer.sortingOrder;
    10.             }
    11.         }
    12.         GetComponent<SpriteRenderer>().sortingOrder = highestSortingOrder + 1;
    13. }
    Thanks to anyone that will take the time to respond !
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,048
    1. Learn to code if you want to make a game, if you really have text, visual scripting also exists.
    2. I suggest to manage this in a manager class. This way you can only increase the sorting order if the object wasn't already clicked (save the currently selected object in a variable).
    2.1 in the long term, try to cache GetComponent to improve performance
     
  3. Dontmakemecodeplease

    Dontmakemecodeplease

    Joined:
    May 23, 2023
    Posts:
    4
    Thank you for your reply, I will get on to learning how to code but this takes a lot of time and effort and for me not to give up on this project I first need to see that my idea is reachable.

    I didn't know about visual scripting in unity i'll do my researches.

    Of course I have no idea what a manager class is but i'll try to find answers, maybe you could explain it to me if you have the time and thanks for the advice on the GetComponent !
     
  4. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,048
    Visual scripting is worse than c# (more limitstions and lower performance), so if you have to pick to learn one and you think it takes similar time, pick c#.

    About the manager class, this simply is a class that manages multiple objects in your case.

    Have a list with all clickable sprites on screen and a spiterenderer called lastclicked
    if a sprite is clicked call spritemanager (or whatever you call it) and say the spriterenderer got clicked, then in the sprite renderer check if the clicked renderer is the same as the last one. If not, increase the layer to 1 higher than the last selected sprite. (Or just 1, and set the previous sprite to 0).

    There are a lot of ways to manage this issue, so find one which works for you and you understand
     
    Dontmakemecodeplease likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    ALL ideas are reachable.

    ALL ideas are also worthless.

    I have BOOKS worth of ideas. All of my ideas are worthless until they become reality.

    What counts is implementation.

    Learn to code or pick up visual scripting, or hire someone to code for you.
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,860
    It's definitely not reachable if you don't know how to code, or lean on the crutch that is Chat GPT.

    You will need to learn how to code proper. No real getting around that.
     
  7. Dontmakemecodeplease

    Dontmakemecodeplease

    Joined:
    May 23, 2023
    Posts:
    4
    Wow, thanks for this great motivational speech !
     
    Kurt-Dekker likes this.
  8. Dontmakemecodeplease

    Dontmakemecodeplease

    Joined:
    May 23, 2023
    Posts:
    4
    Yes, I'll have to change my username for sure...
     
    orionsyndrome likes this.
  9. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108