When I first split the player controlled ships off into AI controlled ships, I attempted to reuse and recycle as much as I had written as possible. In fact, I tried to just make a copy of the player ship and insert a new component called CPUController. That was meant to replace the already written PlayerController script, which I just disabled in the list of components in the inspector. Of course, to make the CPUController script itself, I just started by copying over the PlayerController script and changing the name.
So far, so efficient.
Unfortunately, when I fired it up (tried to build a Web Player file) it wretched and completely failed.
AAARRRGGGHH! I said. (I'm 1/8 pirate it turns out). What errors is this thing throwing?
When I looked, I couldn't see anything that made any sense.
Eventually, I figured out that I had kept a part of the code from the PlayerController that defined OnTriggerEnter() a function called when the game object enters a collider with IsTrigger set to true. When I recycled this code in the CPUController while only disabling the PlayerController, that still left two definition for the function and caused the issues I was seeing.
Too much recycling and reusing code can be a problem.
Another problem was that I was confusing disabling a component with destroying a game object.
In Unity, components are pieces of game objects. Some of them have the ability to be enabled and disabled by code. That's what I was doing to the PlayerController script on the baby AI ship.
But, it could be enabled later, and so the compiler (or whatever that part of Unity is called) still looks at it and notes what's there.
During the course of execution of a game, a game object might be instantiated (born) and destroyed (not just dead, but dead and incinerated with the ashes spread to the etheric winds of the electronic cosmos). There are warnings in multiple places in the documentation that a destroyed game object completely stops. If you have any suspended (using the "yield" keyword) scripts, they will terminate wherever they were at and never proceed if their game object is destroyed. At least I think that's what they meant.
I was confusing disabled with destroyed, and it sunk my recycling project for awhile. But, I figured it out and deleted the PlayerController entirely from the CPU controlled ships. That fixed that issue. Then, I had to realize that another part of my code was specifically calling the PlayerController. That had to be fixed with an if statement to call either the PlayerController or the CPUController depending on what kind of ship was involved.
Luckily, I had foreseen that sort of necessity, and included a variable set to either 1P, 2P, S (for shields), or CPU that I could pass to make that determination.
This comment has been removed by the author.
ReplyDeleteThis post is quite good. Keep sharing the quality content.
ReplyDeleteGame development is quite interesting.