Apress / Devel-2D-Games-Unity

Source Code for 'Developing 2D Games with Unity: Independent Game Programming with C#' by Jared Halpern
https://www.apress.com/us/book/9781484237717
Other
215 stars 53 forks source link

(Ch 7) Player and Enemy Spawnpoint not working #43

Closed graceyh24 closed 3 years ago

graceyh24 commented 3 years ago

I have double-checked my code and compared it to the source code in this repository, but the spawnpoints for the player and enemies are not actually spawning any (visible) characters. While running the game in Game view, I can see in the hierarchy window that a player gameObject and multiple enemy gameObject clones are being created, but none of their sprite images show up in the game itself. I think the problem might lie in either sprite rendering/rendering order or having drag-and-dropped an object instance to a reference variable in the inspector instead of a prefab (or vice versa), but I'm not sure.

graceyh24 commented 3 years ago

@JaredHalpern - Not sure why this is happening - I even substituted the source code for my scripts and have tried out swapping prefabs for gameObject instances in the variable editor in the Inspector.

JaredHalpern commented 3 years ago

Hi @graceyh24 - thanks for mentioning this. Can you check the Z coordinate on the Transform component of the PlayerSpawnPoint? If it's less than the Z coordinate of the Grid, then the player will show up behind the Grid and you'll never see her, despite her GameObject being created. Unity sometimes initializes objects with non-zero Z coordinates when you create them for the first time in the Editor. It's quite annoying and there isn't much we can do about it (that I know of) besides set it up properly when first creating these objects.

graceyh24 commented 3 years ago

I see; that fixed the issue. Thanks so much for your reply (and your amazing book), Jared!

JaredHalpern commented 3 years ago

Thank you for your support @graceyh24 ! :-) Feel free to reach out with any other questions!