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

Question Script Help - Small Fix Tile Puzzle Game

Discussion in 'Scripting' started by unity_94E4DF39DA73609CA9FD, Jun 10, 2023.

  1. unity_94E4DF39DA73609CA9FD

    unity_94E4DF39DA73609CA9FD

    Joined:
    Apr 2, 2023
    Posts:
    3
    So I am writing a function that makes copies of a tile from a randomised array eg. {1,3,4,5,2,7,8,0,6}. The script I have written works for all of the tiles except the empty tile which is held in the array at space 0. I have tried for a long time to try and figure out a fix but I always get stuck. If anyone could help me that would be greatly appreciated :) Thank you.

    Script:
    Code (CSharp):
    1. private void DisplayTiles3x3(int[] RandomisedBoard)
    2.     {
    3.         float slotOffset = 3.33f; // Offset between slots
    4.  
    5.         if (!HasDisplayed)
    6.         {
    7.  
    8.             // Loop through the board array
    9.             for (int i = 0; i < RandomisedBoard.Length; i++)
    10.             {
    11.                 int assetIndex = RandomisedBoard[i] == 0 ? -1 : RandomisedBoard[i] - 1; // Subtract 1 to match array index, set to -1 if it's 0
    12.  
    13.                 // If the asset index is valid and not the empty slot (-1)
    14.                 if (assetIndex >= 0 && assetIndex < AssetPrefabs3x3.Length)
    15.                 {
    16.                     // Calculate row and column based on index
    17.                     int row = i / 3;
    18.                     int col = i % 3;
    19.  
    20.                     // Calculate position based on slot offset
    21.                     float xPos = (col * slotOffset) - 3.33f; // Shift left by 3.33
    22.                     float yPos = (-row * slotOffset) + 3.33f; // Shift up by 3.33
    23.  
    24.                     // Instantiate the asset prefab at the calculated position
    25.                     GameObject instantiatedAsset = Instantiate(AssetPrefabs3x3[assetIndex], new Vector3(xPos, yPos, 0f), Quaternion.identity);
    26.                     Debug.Log("Child of tile created:");
    27.                     Debug.Log(assetIndex);
    28.  
    29.                     // If the asset is the empty space (-1), move it to the position where tile 0 would be
    30.                     if (assetIndex == -1)
    31.                     {
    32.                         float emptyXPos = (col * slotOffset) - 3.33f; // Calculate the position of tile 0
    33.                         float emptyYPos = (-row * slotOffset) + 3.33f;
    34.                         instantiatedAsset.transform.position = new Vector3(emptyXPos, emptyYPos, 0f);
    35.                     }
    36.                     else
    37.                     {
    38.                         // Make the duplicated asset a child of the asset it is a copy of
    39.                         GameObject parentAsset = AssetPrefabs3x3[assetIndex];
    40.                         instantiatedAsset.transform.parent = parentAsset.transform;
    41.                     }
    42.                 }
    43.             }
    44.             HasDisplayed = true;
    45.         }
    46.     }
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Can you be more specific with the .. erm specification?
    For example: I have X, I want to be able to add YZ, in order to get XYZ

    This literally means nothing, that's how vague it is. What is "tile"? What do you mean by "making copies of a tile from a randomised array". What do you mean by "works for all of the tiles except the empty tile which is held in the array at space 0" why and how is this "tile" special in such a way. And again what tile even is and how it relates to integer values?

    Why do we have to read and parse your code, in order to understand what you're doing? When you can easily describe what you want with a single picture, and completely get rid of the bias you're likely to introduce with some solution that you yourself admit to be broken.
     
  3. unity_94E4DF39DA73609CA9FD

    unity_94E4DF39DA73609CA9FD

    Joined:
    Apr 2, 2023
    Posts:
    3
    im really sorry, im still learning what should i do to help people understand?
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Well, don't chit chat, try again! This time, try to read it out loud, see if it's sensible. Rinse and repeat.
    We don't read minds, have no clue of your project, have no clue about what it is that you're making.
    We also have other stuff in our minds. You got to respect all that if you need help.

    What you ask for is probably something I can do with a blindfold -- maybe not -- but until we get to the point where I perfectly understand what is the thing that you need help with, we're just chit-chatting, and I'll be gone in a few moments.

    To get the most out of these forums, be expressive with your thoughts, be clear with what you're trying, illustrate what you've tried, and what you're getting instead. Draw a picture, show a video, make an ASCII doodle I don't care.

    You can't just say "I'm making tiles from a randomised array" because that, on its own, can mean thousands of things. Nearly all games at some point make some tiles from randomised arrays! Be more specific.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,561
    More importantly you want to do things for YOU to understand.

    As Orion notes above, you need to try LOTS of things. Keep after it.

    The easiest approach is to repeatedly ask yourself "Can I...?" with VERY small pieces of the problem.

    Watch how this guy does it:

    Imphenzia: How Did I Learn To Make Games:



    I have no idea what that means but perhaps it is...

    Time 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 or 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.