Search Unity

Sprite GameObjects generated from script not appearing

Discussion in 'Scripting' started by Deper93, Jan 17, 2022.

  1. Deper93

    Deper93

    Joined:
    Jan 10, 2022
    Posts:
    4
    I've been trying to create a function that makes a textbox comprised of a grid of sprites. Though the GameObjects are being created, being properly parented, are positioned correctly, and have the sprites successfully applied--they still don't appear visually. Anyone know what the problem could be?

    Here's the code:
    Code (CSharp):
    1. public class TextGen : MonoBehaviour {
    2.  
    3.     string[] msgArr = new string[] {"This is the   Debug Message!","You set off, ready  to take on anything!"};
    4.     string[] choiceArr = new string[] {"Continue","Yes","No","Attack Them!","Run!"}; //Note: [0] for this is the default option for no-choice messages.
    5.     int[,] msgDim = new int[,] {{14,2},{20,2}};
    6.     public Sprite[] allTextSprites;
    7.  
    8.    
    9.     public void CreateTextBox(int msgID, int[] choices) {
    10.         GameObject TexterBox = new GameObject("TexterBox"); //Note: called Texter insted of Text to avoid accidental name conflicts
    11.         TexterBox.transform.position = new Vector3(0f, 0f, 0f); //Set textbox to origin (center of game screen)
    12.         int columns = msgDim[msgID,0] + 2;
    13.         int rows = msgDim[msgID,1] + 2 + choices.Length;
    14.         GameObject[] TextSprites = new GameObject[columns*rows]; //Make an array to hold all of the sprites for the textbox
    15.         for (int i = 0; i < columns * rows; i++) {
    16.             TextSprites[i] = new GameObject(); //Apparently making an array of GameObjects requires you to make a GameObject for every index anyway, fun!
    17.             TextSprites[i].transform.parent = TexterBox.transform; //Make each object a child of the text box
    18.             float _x = (float)((i % columns * 0.24) - (--columns * 0.12)); //Get X coordinate from column (Note: Sprites are 24*28 and all sprites are set to 100px/unit)
    19.             float _y = (float)(((int)Math.Floor((double)(i / columns)) % rows * 0.28) - (--rows * 0.14)); //Gey Y coordinate from row
    20.             TextSprites[i].transform.position = new Vector3(_x, _y, -5f); //Set position of object based on x and y, in front of everything
    21.             SpriteRenderer _toChange = TextSprites[i].AddComponent<SpriteRenderer>() as SpriteRenderer; //Add sprite to object
    22.             if (i == 0) { //Sprite setter -- I would've loved to do a switch here but c# Doesn't like variables in case statements
    23.                 _toChange.sprite = allTextSprites[46]; //Start Corners
    24.             } else if (i % columns == 0 && Math.Floor((double)(i / columns)) == --rows) {
    25.                  _toChange.sprite = allTextSprites[48];
    26.             } else if (i % columns == --columns && Math.Floor((double)(i / columns)) == 0) {
    27.                  _toChange.sprite = allTextSprites[52];
    28.             } else if (i == (columns * rows) - 1) {
    29.                 _toChange.sprite = allTextSprites[50]; //End Corners
    30.             } else if (i % columns == 0) {
    31.                 _toChange.sprite = allTextSprites[47]; //Start Sides
    32.             } else if (i % columns == --columns) {
    33.                 _toChange.sprite = allTextSprites[51];
    34.             } else if (Math.Floor((double)(i / columns)) == 0) {
    35.                 _toChange.sprite = allTextSprites[45];
    36.             } else if (Math.Floor((double)(i / columns)) == --rows) {
    37.                 _toChange.sprite = allTextSprites[49]; //End Sides
    38.             } //Put logic for what character to display after here
    39.         }
    40.     }
    41.  
    42. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    After this code executes are they in the scene? Are they where you expect? What if you put a new one in yourself and parent it? Do they have what you expect on them? Are you sure the scale isn't zero? Make sure the Z scale is not zero... in case you set something to x,y scale and failed to set z.

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

    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 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 put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

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

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    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
     
  3. Deper93

    Deper93

    Joined:
    Jan 10, 2022
    Posts:
    4
    Thanks for the tips! Currently I'm just using the visual studio unity integration for breakpoints, but I found out that I was actually generating the gameobjects behind the camera! However they're misaligned and less than half have actually generated. I'm trying to use debug.log and console.write, but neither seem to actually output to the console? I think I'm using correct syntax, but instead I'm being given a divide by zero error:
    Code (CSharp):
    1. float _x = (float)((i % columns * 0.24) - (--columns * 0.12)); //Get X coordinate from column (Note: Sprites are 24*28 and all sprites are set to 100px/unit)
    2.             Debug.Log((float)((i % columns * 0.24) - (--columns * 0.12)));
    3.             float _y = (float)(((int)Math.Floor((double)(i / columns)) % rows * 0.28) - (--rows * 0.14)); //Gey Y coordinate from row
    4.             Debug.Log((float)(((int)Math.Floor((double)(i / columns)) % rows * 0.28) - (--rows * 0.14)));
     
  4. Deper93

    Deper93

    Joined:
    Jan 10, 2022
    Posts:
    4
    Figured it out, the debug logs were hidden in console for some reason. Can't quite figure what's throwing divide by 0, however. Neither rows or columns are 0, and it only happens on the third iteration when the _y debug.log exists?
     
    Last edited: Jan 17, 2022
  5. Deper93

    Deper93

    Joined:
    Jan 10, 2022
    Posts:
    4
    It's the decrements, I was using them in place of (- 1)s