- Home /
How to tell which enemys to create in new battle scene
Hey,
I'm working on a turn based combat system. The idea is nothing new you walk along and touch an enemy's collider which triggers the battle arena (-> new scene). Now I am wondering how can I tell which enemy to instantiate inside the new scene based on the enemy I collided with. Right now I'm thinking about creating an specific "empty" gameobject (based on the enenmy I collided) that isn't destoryed on load and somehow check for it in the new battle scene and then decide which enemy should be created based on this "empty" gameobject.
But I don't really know how I could do this and got the feeling that it is probably a stupid idea and that there is a much better way to go for it, since this must be some very common thing to do for role based combat rpgs. I didn't find anything and would be thankful for pointers/ideas/concepts on how to do this (if you want to post script c# would be most useful/understandable).
Thanks a lot.
Couple things you could try.
DontDestroyOnLoad(EnemyGameObject);
http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
As a secondary idea. You could also add an additional camera to the scene and have a "battle arena" hidden until you need it and swap cameras as needed. This would remove the need for you to carry a gameobject into another scene.
Answer by DanSuperGP · Feb 10, 2015 at 12:41 AM
"Right now I'm thinking about creating an specific "empty" gameobject (based on the enenmy I collided) that isn't destoryed on load and somehow check for it in the new battle scene and then decide which enemy should be created based on this "empty" gameobject."
This isn't a bad idea.
If you want data to persist between scenes, the easy way to do it is to make a manager that's set to http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
Then, when you collide with an enemy and you transition between scenes... you can tell that manager important data... like which units to spawn for the enemy, and where on the world map you were when the fight started so you can place the player back in the same spot when the fight is over...
And all sorts of other stuff.
Hey Thanks, for your answer. I already have a GameInformation object which is set to DontDestroyOnLoad. At the moment I only save my players stats in it. Would you use this also to save other information in it? So the idea would be: on collision put data in the GameInformation -> In the battle scene I have a gameObject with a script on it that does different things depending on what was set in the GameInformation? Sounds like I wont need to create a gameobject on collision like my original idea, am I right?
Yeah, you could use the same object, or you could create others.
I could see creating a temporary do not destroy on load object when a fight is triggered... but then taking the data from it and destroying it before going to the map, ins$$anonymous$$d of overloading the GameInformation you already have.
Either way will work fine.
Hey Dan, just wanted to let you know. After some noob struggles I finaly got what I wanted. The Collider checks for other colliders inside it that are on the layer enemy. This way I create an array of all enemys inside the collider. From this array I create another string array that stores the typeOfEnemy string that each enemy has. This gets send to the EnemyInfoTemp which is created the moment the player triggers the collider and is set to don't destroy on load.
In the new loaded battle scene. I check the saved string array in the EnemyInfoTemp with a foreach loop and instantiate the right enemy for each string in the array.
Tada it works :) program$$anonymous$$g drives me crazy but when it finaly works it's awesome!!