- Home /
Efficient Way to Create/Destroy and Switch Between Player GameObjects
Hello everyone. In my game the player starts in a small spaceship called a cruiser. If they fly to a spacestation they can purchase other ships and store them in their hangar. Let's say there are 2 ship types, "cruiser" and "battleship". I have a prefab for each, with all of the basic attributes set in their "void start()" function. What I need to accomplish is having the player be able to purchase a battleship, get into it (thus storing the cruiser in the hangar, with all of its variables intact), fly it around, potentially have it explode, and have to repurchase it with all of its variables resetting to base values. This would be super easy if I could just "SetActive true/false" the ships as needed, but I find I can't "SetActive(true)" something once it has been "SetActive(false)".
Is there an easy and efficient way to do this? All of my ship prefabs have the character controller script on them, so as long as just one of them is active at a time, I should have no problems. Any help would be greatly appreciated.
Answer by Andy Lee · Aug 16, 2014 at 06:05 AM
May I know why you can't SetActive(true)? Is it because you have destroyed your gameObject?
One thing I have noticed is "with all of its variables resetting to base values"
If I understand it correctly, you want the attributes reset when the object becomes active again. Then, void OnEnable() will be what you need. It is called every time the object becomes active. Note: the object is also activated when the game starts, so you don't need Start().
Or, I would write a custom function for resetting the attributes so that I can call it whenever I like.