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 hsck98 · Mar 07, 2019 at 07:55 AM · instantiateeventstaticload scenehealth bar

Static event does not trigger during initial loading of scene

Currently, I am trying to instantiate a new health bar for each new gameObject with a health system. The game has 12 components with a health system script in them. However during runtime, "Before" and "After" are printed 12 times alternatingly but no "HealthSystem added" is printed. One thing to note though whenever I duplicate any of these gameObjects, the all three Debug.Logs are triggered and the HealthSystem is added and the health bar appears on screen. I just do not understand why it does not work during the initial load of the scene. Could someone explain what the problem is and maybe try to give the solution to this kind of problem? Thank you very much, I've struggled to find any answers online and ive been on this problem for a long time :(

public class HealthSystem : MonoBehaviour {

 public static event Action<HealthSystem> OnHealthAdded = delegate { };
 public static event Action<HealthSystem> OnHealthRemoved = delegate { };

 private void OnEnable()
 {
     Debug.Log("Before");
     OnHealthAdded(this);
     Debug.Log("After");
 }

 private void OnDisable()
 {
     OnHealthRemoved(this);
 }

}

public class HealthBarController : MonoBehaviour {

 [SerializeField] private HealthBar healthBarPrefab;

 private Dictionary<HealthSystem, HealthBar> healthBars = new Dictionary<HealthSystem, HealthBar>();
   
 private void Awake()
 {
     HealthSystem.OnHealthAdded += AddHealthBar;
     HealthSystem.OnHealthRemoved += RemoveHealthBar;
 }

 private void AddHealthBar(HealthSystem healthSystem)
 {
     Debug.Log("HealthSystem added");
     if (healthBars.ContainsKey(healthSystem) == false)
     {
         var healthBar = Instantiate(healthBarPrefab, transform);
         healthBars.Add(healthSystem, healthBar);
         healthBar.SetHealthSystem(healthSystem);
     }
 }

 private void RemoveHealthBar(HealthSystem healthSystem)
 {
     Debug.Log("HealthSystem destroyed");
     if (healthBars.ContainsKey(healthSystem))
     {
         Destroy(healthBars[healthSystem].gameObject);
         healthBars.Remove(healthSystem);
     }
 }

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by xxmariofer · Mar 07, 2019 at 08:37 AM

i would say your problem is with the script execution order, OnEnable isnt like start that is excuted after all the awakes. the awake is called just before the OnEnable, but not before all the OnEnable of all objects, you need to make the healthBarController execution order faster than the healthbarsystem script, the script orther is right now is something like this

  HealthSystem [Awake -> OnEnable(calls your events with still nothing subscribes)] -> HealthBarController[Awake (your subscrubed right now)-> OnEnable]

for changing execution order is Edit->Project Settings -> Script Execution Order

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

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

125 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

Related Questions

how to instantiate in an extension? 1 Answer

Need my boolean to apply to the instance of my object instead of class 1 Answer

Trying to run 5 instances of a static method at once to do an action, probably a dumb thing to do? 0 Answers

Gamecontroller "Awake" instantiates other objects - Event order question 2 Answers

how to spawn on random postion but non on static positions 0 Answers


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