Attemped to use GameObject.GetComponent().bounds.size.y to find the height of a sprite, but weird glitch happening when I do.
coding in c# in unity. So, here's some background: I had a bunch of sprites that were set out in a grid like pattern, all children of another background sprite, which was again the child of another sprite which had the script controlling everything about these things. Now, the background and all the other sprites had no scripts on them as of yet (just the BoxCollider2D), and the controller script only referenced the background to find it's position - there was not a single line of code that changed anything about the background anywhere.
So, I didn't realise this at the time, but I had the positions a bit messed up, and sprite 1 was at the bottom left, which was why I was having trouble getting the grid's top-left to stay in the same spot with different amounts of sprites. Each row added was going up, so I tried doing some whacky bit of code to make it work anyway, which involved that bit at the top. Now, this seemed to work - for about a millisecond, until for seemingly no reason at all, the entire grid, and the background behind it, slid slightly to the left, and stayed there. To reiterate, that code should have given out a specific value for each sprite that should under no circumstances change, and the background doesn't even have any code affecting it!
And yet, it slid. The unity inspector registered no change in x position of any of the sprites, the background or even the parent object when it happened. Also, this only happened, inconceivably, when there are more than 10 sprites in the grid - or over 2 rows, since each row can only have 5 in it.
So, what I'm asking is this: Why is this happening? (This isn't really urgent, since I've figured out a better alternative already, but I kinda wanna know what happened)
Answer by jinxs2011 · Sep 26, 2018 at 09:51 AM
I found the reason for this happening, and it's not a glitch. It was simply the character, which the camera was centered on, getting pushed away by th colliders that appeared on top of it, making the grid appear as if it slid away, as its position is not relative to the character.