first of all sorry for my bad english(I speak spanish)
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.
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
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
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.
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");
}
}
}
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.
Sorry, didn’t read your message properly with my (first try) post.
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
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.
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
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
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)
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”.
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. It’s all connected. lol