Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 binskiboys3 · Jan 13, 2019 at 06:56 AM · scorestaticscore systemmethodnon-static

Why is my kill method not running the score system method call?

I have used Debug.Log(""); to make sure the score value is actually a number and not just zero. It is the number which i assigned it. I have also put a Debug.Log(""); on the other side of the call to see if it actually increases score. The Debug never appears in console. Please help me as im new to unity and im trying to code without a tutorial but I often look up fixes.

Code for my Enemy health, also my update to kill is on line 31 and the call is on 34:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class EnemyHealthManager : MonoBehaviour { public float maxHealth; public float currentHealth;

 private int killCounter;
 public int scoreValue;
 private int standerdCounter;
 private int tankCounter;
 private int fastCounter;


 public Image healthImage;
 // Start is called before the first frame update
 void Start()
 {
     currentHealth = maxHealth;
     fastCounter = 0;
     standerdCounter = 0;
     tankCounter = 0;
 }

 // Update is called once per frame
 void Update()
 {
     if (currentHealth <= 0)
     {
         Destroy(gameObject);
         ScoreSystem.score += scoreValue;
         EmemyCheck();
         killCounter += 1;
     }
     
 }
 public void EmemyCheck()
 {
     if (maxHealth == 50)
     {

         standerdCounter += 1;
         
     }
     else if(maxHealth == 30)
     {
         fastCounter += 1;
     }
     else
     {
         tankCounter += 1;
     }
 }


 public void HurtEnemy(int damage)
 {
     currentHealth -= damage;
     healthImage.fillAmount = (currentHealth / maxHealth);
 }

}

code fore my score system:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class ScoreSystem : MonoBehaviour { public static int score; Text scoreText;

 // Start is called before the first frame update
 void Awake()
 {
     scoreText = GetComponent<Text>();
     score = 0;
 }

 // Update is called once per frame
 public void Update()
 {
     scoreText.text = "Score: " + score;
 }

}

PLEASE HELP I'M SO TIRED OF THIS. ALSO THERE ARE NO RUN-TIME OR COMPILE ERRORS!! However before I was getting a non-static error for my method call, but my variable that was being called was static so i assumed it just a bug and restarted my unity project. The error didn't show anymore but it still doesn't work. It could be the same issue but just not showing? thanks for reading.

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 sh_code · Jan 13, 2019 at 08:23 PM

How about you FIRST INCREASE THE SCORE, and THEN destroy self? ;)

Destroy is a weird one, you don't have a guarantee WHEN it will happen, whether immediately, or on the end of current frame, or sometime within the next few frames.

BUT, in any case, FIRST you want the object to do everything it needs to, and THEN you want it to say to the engine it wants to be destroyed.

TL;DR : move the Destroy call so that it's the LAST LINE of the function. ALWAYS.

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

101 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

Related Questions

How to check if an object has rotated around itself (360° in x,y,z) 1 Answer

How to give points after certain number of seconds?,How to give points after a certain number of seconds? 1 Answer

my code is suppose to store high score and its storing recent score insted and it wont stop if game over and it has an score counter as well plzz help 0 Answers

Score and Highscore - Highscore not saving, loading, or displaying. 0 Answers

Assessing variables from other scripts using an accessor seems inelegant. Please explain. 3 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