The Ghost of the Player-Object

This is a really strange bug I experienced, which I don’t entirely understand. To me it felt like a ghost story and I’d really like to share it with everyone, so I’m trying to make this as easy to understand to non-coders as possible!

Screenshot_3

This “KillZoneScript” is attached to a river, which is supposed to kill the player when they touch it. It does two things: The first is that it find a reference (basically a hyperlink) to the  Player-Object when the game is started. The second is that -when the player touches the river- it tells a script that is attached to the Player-Object to run a method in it, which will do the actual death animation etc of the Player.

Now while I was working on implementing an improved death animation of sorts it bugged out -the Kill method wouldn’t be called- , so I did some reconnaissance:

Screenshot_6

Using lots of print functions, and trying to make the program tell me exactly WHAT happens and WHERE the bug is located, I found out that the reference to the player was wrong. This version of the method told me the object it was referencing is named “Player(Clone)” instead of “Player”,as it should be named. So I started suspecting that I might accidentally have tagged another object as “Player” which would explain this problem (the other object would be found instead of the correct one). Eventually, I made the program find all the GameObjects tagged as “Player” and tell me their names:

Screenshot_1

I learned that there were indeed TWO GameObjects tagged as “Player”, which are the “Player(Clone)” (gobjo[0]) that I noticed was weird, as well as the Object named “Player” (gobjo[1]), which had to be the correct object!

Now here is where the story moves from being like any annoying bug hassle to being a ghost story: The “Player(Clone)” object was nowhere to be found! In Unity, ALL objects are displayed in the so called “Hierarchy”, as below:Screenshot_7

And no matter what I did, searching manually or through the search function, whether it was while the game was playing or not, the “Player(Clone)” object would not show up!

I had one idea left, which I thought would serve to confirm my suspicion of the “Player(Clone)” object to be the one to ruin my code, so I changed the method to delete that object:Screenshot_8

Since I knew that “gobjo[0]” contained the evil Clone-Spirit-Demon, I made the method destroy that object during play, so I could see whether that would allow the rest of the method to work. I didn’t quite figure out yet how I would fix it in the long run, since anything that happens during play will be reset once the game is stopped. However as it turned out, I didn’t need to! Mysteriously, the “Player(Clone)” object was destroyed, and remained gone even after leaving play-mode!

And so the spirit of the Player-Object was banished to the depths of the GarbageCollector, and was never heard of again (?).

This was a bit of a weird post, but I really felt the need to share this. I hope I could entertain some of you, maybe even someone that is not an experienced coder!

 

Follow up, 4 days later:

Short follow-up:
The cloning happened again, while we were in a group meeting, and I eventually found out that the cause is somewhere in the mechanim (this user had a similar problem)
It can be fixed by just restarting the editor (how boring :P)

 

Leave a Reply

Your email address will not be published.