Hello over the last few weeks i have been developing a 2D Roleplaying game similar to Runescape / WoW.
I have enemy controllers and health and such but what ide like to do is once you kill them after a certain amount of time they respawn in the same region on the map.
Is there a way about doing this?
Also im looking to have them drop items and thus have my player be able to pick them up. Now obviously i wouldnt want say a skeleton to drop bones every single time. so is there a way to do this where they drop an item randomly?
This is all made with sprites and a tile map using Tiled2Unity.
Much thanks.
Adam.
Make a script and put it on an empty object. Place the object in the area you want to spawn the enemy. In the script put a GameObject variable and in the editor drag your enemy prefab into it. Make a function (we’ll call it SpawnEnemy) in the script that instantiates a copy of the enemy prefab object at a random position (use float variables to define the position ranges minimum and maximum x axis spawn points by using Random.Range (minXPos, maxXPos); and do the same for y (I’m assuming you’re doing top-down 2D.) Put a bool in the script named something like enemyExists. When you instantiate the enemy set the bool to true. When the enemy gets killed have its script change the bool in the spawners script to false. In the update function have something along the lines of:
void Update () {
if (!enemyExists) StartCoroutine ("SpawnTimer")
}
Then make a coroutine along the lines of:
IEnumerator SpawnTimer () {
yield return new WaitForSeconds (10);
SpawnEnemy ();
}
Now when your enemy gets killed the enemy will change the enemyExists bool to false, telling update to trigger the SpawnTimer coroutine which waits 10 seconds and then spawns another enemy at a random position within the ranges you gave it.
As for your second question, you’ll need to make a prefab out of the droppable objects and make a script with a list of the droppable object prefabs, then when the enemy is killed choose a random object from that list (or perhaps have it choose between an object from the list or no object at all) and instantiate the object at the position the enemy just occupied and have a script on the object that lets the player pick it up (making and spawning a prefab for it will depend on your method of pickup - does the item get dropped and the player has to touch it, does the item move toward the player and then disappear when it reaches it, is the item even shown or does it just go directly into the player inventory, etc.)
Hope that helps. 
Nice answer above. I might add you could also skip the Update check, if the enemy script, the one changing the bool from the above post, were to call a method to spawn. That method could just start the coroutine (wouldn’t even need the bool). However, if you had some group of enemies, and you only wanted to spawn more after the whole group was dead, then some modification would be needed.