- Home /
Get XP on enemy death
Hello.
I was stucked with some weird problem for 2 days. Sometime ago I started to implement level system. I wrote it and wanted to write script for gaining experience on enemy death. I used event system to do this and subscribe to it, but I simply can't trigger it in game upon enemy death.
Unity says: "Object reference not set to an instance of an object" at line 24 in BloodSucker script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class BloodSucker : MonoBehaviour {
public int health;
public GameObject effect;
public GameObject corpse;
public event EventHandler OnBloodSuckerKilled;
public LevelSystem levelSystem;
private void Update()
{
if (health <= 0)
{
Instantiate(corpse, transform.position, Quaternion.identity);
Instantiate(effect, transform.position, Quaternion.identity);
Destroy(gameObject);
OnBloodSuckerKilled.Invoke(levelSystem, EventArgs.Empty);
}
}
public void TakeDamage(int damage) {
health -= damage;
}
public void XP_OnBloodSuckerKilled(object sender, System.EventArgs e) {
levelSystem.AddExperience(100);
Debug.Log("This code working");
}
public void SetLevelSystem(LevelSystem levelSystem)
{
this.levelSystem = levelSystem;
OnBloodSuckerKilled += XP_OnBloodSuckerKilled;
}
}
You can say that I should make new LevelSystem, but I can't, cause I have one more script that using my levelSystem, so I've created code that sends them original levelSystem in SetLevelSystem
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestingLvlSys : MonoBehaviour
{
public ExperienceBarUI experienceBarUI;
public BloodSucker bloodSucker;
LevelSystem levelSystem = new LevelSystem();
public void Awake()
{
experienceBarUI.SetLevelSystem(levelSystem);
bloodSucker.SetLevelSystem(levelSystem);
}
}
So, I need to Invoke my XP_OnBloodSuckerKilled when enemy killed. How can I do that. Thank you in advance for answers.
I think it is OnBloodSucker$$anonymous$$illed that is "not set to an instance of an object"...
Couldn't this be done without events by a direct call to levelSystem.AddExperience(100) ins$$anonymous$$d of OnBloodSucker$$anonymous$$illed.Invoke()...?
Hi, @rh_galaxy, thanks for comment. Yeah, you are right that it is OnBloodSucker$$anonymous$$illed, because it's 24 line. However, I can't do this with only call, because levelSystem.AddExperience needs levelSystem to start work with XP =/
Answer by ADIS1 · Mar 29, 2020 at 01:15 PM
I solved this problem!
I just send to TestingLvlSys death of the Bloodsucker and call function addExperience in it.
Answer by logicandchaos · Feb 22, 2020 at 02:16 PM
You will still need to assign level system in BloodSicker script. You created a new one in TestingLvlSys but if you want that to be the same one in BloodSicker then you have to assign it to that. To get XP_OnBloodSuckerKilled called when enemy dies you just need to call it in your if (health
Hi, @logicandchaos, thanks for your reply. I have already assigned Level system this.levelSystem = levelSystem;
and I can't simply call XP_OnBloodSucker$$anonymous$$illed, because to call AddExperience(n) it must take levelSystem anyway. So, if I delete all events and simpy call fucntion, Unity will say: "Object reference not set to an instance of an object" in line where AddExperience is located.
Your answer
Follow this Question
Related Questions
4.6 UI EventSystem Handler Redirection? 0 Answers
Animation Crossfading and Events (Legacy) not triggering at times 0 Answers
OnApplicationFocus called twice - need to stop input 0 Answers
Animation stops playing as soon as AnimationClip.AddEvent is used. 0 Answers
Deactivating object makes events null. What is the best workaround? 1 Answer