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 /
  • Help Room /
avatar image
0
Question by xXSilverswordXx · Mar 14, 2017 at 11:17 PM · errorspriterendererhealthbar

Strange error i dont understand because i am a child

I have no idea what is wrong. Please help meh.

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class PlayerHealth : MonoBehaviour {
 
     private int Playerhealth = 3;   // This string holds the players current health value. 
     private int DmgCD = 0;    // This string detects if the player can currently recieve damage
     public Sprite[] Healths;
     public GameObject healthbar = new GameObject();
 
     void Start () {
         //healthbar.AddComponent<SpriteRenderer>();
         Healths = Resources.LoadAll<Sprite>("Healths");
     }
 
     void Update()
     {
         if (Playerhealth == 3)
         {
             healthbar.GetComponent<SpriteRenderer>().sprite = Healths[3];
         }
         if (Playerhealth == 2)
         {
             healthbar.GetComponent<SpriteRenderer>().sprite = Healths[2];
         }
         if (Playerhealth == 1)
         {
             healthbar.GetComponent<SpriteRenderer>().sprite = Healths[1];
         }
         if (Playerhealth == 0)
         {
             healthbar.GetComponent<SpriteRenderer>().sprite = Healths[0];
             Playerhealth = 3;
             //Application.LoadLevel("Lose"); (Remove the comments from this when the lose level is finished)
         }
     }
 
     private IEnumerator HealthTracker()
     {
         while (true)
         {
             yield return new WaitForSeconds(2f);
             StopCoroutine(HealthTracker());
         }
     }
 
     void OnCollisionEnter2D(Collision2D col)         // When getting hit by a projectile with the enemy tag
     {
         if (col.gameObject.tag == "Missile")
         {
             print("damage recieved");
             if (DmgCD == 0)                           //If the player is allowed to be damaged
                 DmgCD = 1;                            // Make it so the player can no longer recieve new damage
                 Playerhealth = Playerhealth - 1;
                 StartCoroutine(HealthTracker());
             }
         }
     }
 

The script is attempting to change a health bar depending on the players health values. The script also controls the health values and collisions.

Error: Internal_CreateGameObject is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'PlayerHealth' on game object 'healthbar'.

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
1

Answer by Commoble · Mar 14, 2017 at 11:43 PM

Error: Internal_CreateGameObject is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'PlayerHealth' on game object 'healthbar'.

This is the important part:

(or instance field initializer)

This is an instance field initializer:

 public GameObject healthbar = new GameObject();

Can't do that there because of the way Unity works. Initialize your healthbar gameobject in Awake or Start. Alternatively, you could create your healthbar object in the scene editor and link it to your script in the inspector.

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

111 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

Related Questions

Unexpected Symbol 'float' 0 Answers

what is wrong here trying to make my player take damage by a projectile 0 Answers

Every GameObject that has a rigidbody, won't stay at Z 0 and I don't know why 0 Answers

BCE0043 unexpected token: ")"? 1 Answer

Default Scene not loading 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