Help with my "drop system"(loot of enemys)

first of all sorry for my bad english(I speak spanish) :confused:
I’m trying to detected the enemy hp in 0… I’m debuging hp in console but when it reaches 0 my script can not detect it. I need detect hp in 0 to drop some “items”.I tried to wait a second before destroying the enemy but the drop were clones a lot of times.
Here i will upload some photos of my code.
Thanks in advance. :slight_smile:

this is the code when the enemy hp takes damage

public void Attacked(int dmg){
       
        hp=hp-dmg;
        if (hp <= 0) {
            Destroy (gameObject);
        }
    }

this is the code to drop items when enemy hp goes down to 0

void Update ()
    {
        Debug.Log (enemy.hp.ToString());
        if (enemy.hp==0)
        {
            Instantiate (dropUno.prefab,transform.position,transform.rotation);
            Instantiate (dropDos.prefab,transform.position,transform.rotation);
            Instantiate (dropTres.prefab,transform.position,transform.rotation);
        }
           
    }

and this is the code of attack to inflict damage

public int Dmg=1;
    void OnTriggerEnter2D(Collider2D col){
        if (col.gameObject.tag == "Enemy") {
            Dmg = Dmg;
            col.gameObject.SendMessage ("Attacked",Dmg);
        }
       
    }
}




This is a link that explains how you can post your code in the thread. It looks good & is easy for people viewing the thread to examine. Check it out and post your code here: Using code tags properly

:slight_smile:

1 Like

Thanks you, Already update the code

I think you should move your dropped items into the code where the enemy takes damage. Then, remove the code in your Update() method, as it won’t be needed. :slight_smile:

1 Like

I tried it but I only get a error x.x. when I arrive to my house I set the code to try it and

I already tried that but i get a error xdd now I’m try that again to look the error

NullReferenceException: Object reference not set to an instance of an object
Enemy.Attacked (Int32 dmg) (at Assets/Scripts/Enemy.cs:149)
UnityEngine.GameObject:SendMessage(String, Object)
attack:OnTriggerEnter2D(Collider2D) (at Assets/Scripts/attack.cs:10)

this is the error i try find it in Google but i did not know how fix this.

this code throw for console “null”… i think that the object was already referenced x.x i have in the beggining of code “private Drop drop”

    public void Attacked(int dmg){
       
        hp=hp-dmg;

        if (hp <= 0) {
            if (drop) {
                drop.ItemDrop ();
                Destroy (gameObject);
            } else {
                Debug.Log ("null");
            }
        }
    }
    public void ItemDrop()
    {

        Debug.Log (enemy.hp.ToString());
        if (enemy.hp <1)
        {
            Instantiate (dropUno.prefab, transform.position, transform.rotation);
            Instantiate (dropDos.prefab, transform.position, transform.rotation);
            Instantiate (dropTres.prefab, transform.position, transform.rotation);

        }

    }

Is the enemy instance inside your drop object set properly? Seems like your error is at the Debug.Log(enemy.hp…) line since that doesn’t show in the console. It also seems odd to double check the hp in ItemDrop() since obviously it’s being called only during death.

1 Like

Sorry, didn’t read your message properly with my (first try) post. :slight_smile:

if you have private Drop drop, how do you create the reference for starters?
I figure this should never be null, in this script. Make sure it’s set properly.

I agree that you shouldn’t need to re-check the health value in your other method :slight_smile:
Just execute the code to instantiate your drops.

In the future, it’s helpful if you can point readers to the line code that’s responsible for the error message. :slight_smile:

1 Like

Oh yes you are right i didn´t noticed it(of double check hp) and because of enemy configuration I think it’s fine because i try this in Updathe method and throw for consolo the enemy hp until this reaches 1 x.x when reaches 0 the enemy was destroyed and the hp was not detected :confused:

Sorry I had problems with my internet :confused: ammm how I can create a reference to drop? idk how make it(I’m nooby xd)

Well, I don’t know either, yet. :slight_smile:

Is the Drop script on the same game object or a different one?

If it’s on the same:

void Awake() {
   Drop = GetComponent<Drop>();
  }

If it’s on a different game object, then add this to its declaration:

[SerializeField]
Drop drop; // assign this value from drag & dropping it in the inspector

Suggestion for you: If you are that new, and not familiar with how to create references like that in such, try some of the introduction (Unity and Essentials) tutorials found in the Learn section: Learn
They’ll come in handy throughout your entire time with Unity/game making :slight_smile:

1 Like

Ohhh maybe i need create a new gameObject to drops? something like that create a prefab with drop script?
GameObject
Drop1
Drop2
Drop3

and here instantiate items to drop?

PD:in the attached photo I show the drop and enemy script.
PD2:right now my Drop script is not assigned to any object(maybe this is the reason why throw null for console)

3356647--262698--4.png

ty for you Suggestion :slight_smile: I’m following some tutorials in YT and reading a little docs of unity (I’m a rlly bad lector xd)

Probably an easy way to alter your setup is to simply attach the Drop script to the enemies.
Then, do the getcomponent in awake (or drag and drop it to the variable in the inspector; this would work on a prefab, too).
Now, it shouldn’t be null and it should have references to your “items”.

1 Like

That’s fair. I can understand wanting to learn familiar game designs and such.
Sometimes the ‘basics’ sound boring, but I promise that’s how you get to other more “interesting” stuff. :slight_smile: It’s all connected. lol

1 Like

lol yeah

I advance slowly but surely (or so I try lol)

the problem with this is when i try attach the Drop in:

[SerializeField]
Drop drop; // assign this value from drag & dropping it in the inspector

I can’t drag and drop in this field x.x

Cool :slight_smile: Well, let us know if the suggestions work for fixing up your script… And enjoy your time making the game :slight_smile:

1 Like

First put the drop script on the enemy. Then drag the enemy to that slot (and it will automatically pick the drop script from the object) :wink:

1 Like

3356680--262711--5.png
is Magic :ooo automatically pick the script Enemy(Drop):hushed:

but when I attack the enemy
3356680--262712--6.png
the script disappears x.x.x.x.x.x.x this is very sad I’m was excited

lol. Did you maybe try to set ‘drop’ somewhere else in the code? Somewhere that produced null, perhaps?

1 Like