Search Unity

Enemy random item drop

Discussion in 'Scripting' started by Clavery, Jan 28, 2014.

  1. Clavery

    Clavery

    Joined:
    Jan 8, 2014
    Posts:
    8
    Hello. I have 20 enemy and 5 item. I can kill they by shooting. I want to randomly add 5 item to 20 enemy. When enemy is died if it have item drop them.
    I searched on the internet but could not find script/code.
    Can someone help me for this script ?

    Sorry for my bad english.
     
  2. Mirace

    Mirace

    Joined:
    Nov 29, 2010
    Posts:
    481
    Do you have any code ? No one is providing you a full code , check you tube for tutorials if you are new :)
     
  3. Dave-xP

    Dave-xP

    Joined:
    Nov 8, 2013
    Posts:
    106
    if your items are prefabs then you can use something like this
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic; // That is important
    4.  
    5. public class die : MonoBehaviour {
    6.  
    7.     public List<Transform> items = new List<Transform> ();
    8.  
    9.     void dropRandomItem () {
    10.         Instantiate(items[Random.Range(0,items.Count-1)],transform.position,Quaternion.identity);
    11.     }
    12. }
     
    Michael_Berna and godwave97 like this.
  4. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    So you have 20 enemies in the scene, each enemy has 5 items.

    You have x amount of items and you want to give each enemy 5 random items from these?

    You want the enemy to drop any items it has when it is killed?

    How are the items going to be shown on the player and enemy, are they actual models / sprites that the enemy carries or are they going to be shown in an inventory type thing.

    Are we talking 2D or 3D.
     
  5. Clavery

    Clavery

    Joined:
    Jan 8, 2014
    Posts:
    8
    @softwizz My enemy is soldiers. If enemy is died I want to drop ammo from enemy. But max drop ammo total 5.
    When enemy died drop ammo if it has.
    20 enemy.
    5 ammo.
    1 enemy is died and it has ammo.
    Total ammo 4.
    Im making 2d game.
     
  6. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    so you have 20 enemies and only 5 ammo.

    So only 5 of the enemies will have ammo to drop.

    If the player kills an enemy with ammo then the ammo will be dropped.

    How are the enemies being spawned, are they all done at once or over time.

    What code have you got so far.
     
  7. Clavery

    Clavery

    Joined:
    Jan 8, 2014
    Posts:
    8
    @softwizz

    Here is my enemy spawn code

    Code (csharp):
    1. var spawnPoints : Transform[];
    2. var enemyPrefabs : GameObject[];
    3. var amountEnemies = 20;
    4. var yieldTimeMin = 2;
    5. var yieldTimeMax = 5;
    6. var i : int;
    7.  
    8. function Start () {
    9.     Spawn();
    10. }
    11.  
    12. function Spawn () {
    13.     for (i=0; i<amountEnemies; i++){
    14.         yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));
    15.         var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)];
    16.         var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)];
    17.         Instantiate(obj, pos.position, pos.rotation);
    18.        
    19.     }
    20. }
    21.  
     
  8. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    Ok, now what is ammo, is it a boolean in the enemy script to say it has ammo, is it a gameobject that the enemy carries around.

    How are you going to show the enemy as having ammo.