Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Error instantiate item at enemy dead

Discussion in 'Scripting' started by Paykoman, Mar 14, 2015.

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi guys i already implement my item DB, my item drop when enemy has 0 hp, i start game fine but when enemy die, it drop the item properly but i get this erro message:

    NullReferenceException: Object reference not set to an instance of an object
    DropItem.UseDropItem () (at Assets/Scripts/DropItem.cs:32)
    CharacterManage.GetHit (Int32 playerDamage) (at Assets/Scripts/CharacterManage.cs:28)
    Player.impact () (at Assets/Scripts/Player/Player.cs:44)

    This is my scripts, can anyone help me plz?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent (typeof (DropItem))]
    5.  
    6. public abstract class CharacterManage : MonoBehaviour
    7. {
    8.     public string characterName;
    9.  
    10.     public int maxHealth;
    11.  
    12.     public int health;
    13.    
    14.     public int damage;
    15.  
    16.     public float range;
    17.  
    18.     //Attack spped is described one attack per second
    19.     public float attackSpeed;
    20.  
    21.     public void GetHit(int playerDamage)
    22.     {
    23.         health = health - playerDamage;
    24.  
    25.         if (health < 0)
    26.         {
    27.             health = 0;
    28.             this.GetComponent<DropItem>().UseDropItem();
    29.         }
    30.     }
    31.  
    32.     protected abstract void Attack();
    33. }
    34.  
    Code (CSharp):
    1. /// <summary>
    2. /// Drop item.
    3. /// This script use to control a enemy drop item
    4. /// </summary>
    5.  
    6. using UnityEngine;
    7. using System.Collections;
    8. using System.Collections.Generic;
    9.  
    10. public class DropItem : MonoBehaviour {
    11.     [System.Serializable]
    12.     public class Drop{
    13.         public int item_id;
    14.         public float percentDrop;
    15.     }
    16.     public GameObject item_Obj_Pref;
    17.     public int gold_min, gold_max;
    18.     public List<Drop> item_Drop_List = new List<Drop>();
    19.  
    20.     public float powerImpulse = 1;
    21.  
    22.  
    23.     public void UseDropItem(){
    24.      
    25.      
    26.         for(int i = 0; i < item_Drop_List.Count; i++){
    27.             float percentResult = Random.Range(0,100);
    28.             if(percentResult < item_Drop_List[i].percentDrop){
    29.                 GameObject go = (GameObject)Instantiate(item_Obj_Pref,transform.position+(Vector3.up),Quaternion.identity);
    30.                 go.GetComponent<Rigidbody>().AddForce(new Vector3(Random.Range(-1,1)*1.5f*powerImpulse,1*3*powerImpulse,Random.Range(-1,1)*1.5f*powerImpulse),ForceMode.Impulse);
    31.                 Item_Obj item_obj = go.GetComponent<Item_Obj>();
    32.                 item_obj.item_id = item_Drop_List[i].item_id;
    33.                 item_obj.SetItem();
    34.                 if(item_obj.gold){
    35.                     item_obj.gold_price = Random.Range(gold_min, gold_max);
    36.                 }
    37.             }
    38.         }
    39.     }
    40.  
    41.  
    42. }
    43.  
     
  2. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    The component Item_Obj isn't attached to the object, or the drop list is empty. Specifically at line 31. That's returning a null.

    Make 100% sure that there's an Item_Obj attached to the gameobject you're instantiating, and that there's at least one object in the drop list.
     
  3. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Tell me just one thing its 100% necessary i hv all drop items in Prefabs? So i can instantiate them right? Cuz im get so confused now... So my enemy has script drop item that ask gameobject to instantiate. So i need create a prefab to instantiate and attach the script item_obj to that prefab right? But the item_obj ask for Game object too... Jesus 5am and im going stupid and crazy loool
     
    Last edited: Mar 14, 2015
  4. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    On line 31 you are requesting an Item_Obj script from the object you're instantiating. That could be returning null.

    But, if your list of drops is empty, line 32 will request the item_Drop_List with an index of zero. If there are no drops at all, then it will return a null as well.

    So check both of those. Make sure that the item_Drop_List contains at least one item, and that the object your are instantiating has an Item_Obj script.