Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ADIS1 · Feb 22, 2020 at 11:13 AM · eventdeathinvokeexperience

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rh_galaxy · Feb 22, 2020 at 03:59 PM 0
Share

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()...?

avatar image ADIS1 rh_galaxy · Feb 22, 2020 at 04:55 PM 0
Share

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 =/

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ADIS1 · Feb 22, 2020 at 02:37 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

126 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges