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 wail13 · Jul 29, 2019 at 04:16 PM · scorescore systemadddoesntwontwork

Why cant i add to my score when my enemies die

In my game when you shoot the enemies i want the points to increase accordingly. this is my player script the score managerusing System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI;

public class Player : MonoBehaviour { public int health; public int score; public Text text;

 void Start()
 {
  
 }


 // Update is called once per frame
 void Update()
 {
     if (health <= 0)
     {
         Destroy(gameObject);
         SceneManager.LoadScene(thisScene);
     }

     if (GetComponent<Enemy>().health <= 0)
     {

         score = score + 10;

     }

     if (GetComponent<TriangleEnemy>().health <= 0)
     {

         score = score + 5;

     }

     if (GetComponent<CircleEnemy>().health <= 0)
     {

         score = score + 1;

     }

     text.text = score.ToString();

 }

}

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 Atenrev · Jul 29, 2019 at 05:16 PM

First of all, if I'm understanding it well, you are getting the enemy component from your player's class.

Secondly, you must think of giving another focus and don't check in the update the life of the enemy to perform the actions, but create a public function to be called by the player that do the stuff and check the life only when the player hits the enemy.

Comment
Add comment · Show 5 · 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 wail13 · Jul 29, 2019 at 06:43 PM 0
Share

@Atenrev do you mean make a new function and then put the stuff thats in update in the new function and then call that function in update? Also the player never touches the enemy it "shoots" projectiles at the enemies the spawn in and run towards the player.

avatar image Atenrev wail13 · Jul 29, 2019 at 09:04 PM 0
Share

No, I mean to create a function that is called in the OnCollision. I guess you are using one to tell the enemy that it was touched. So, when the bullet touches the enemy, it should trigger an OnCollision on the enemy and then, call all the stuff you writted in that function. It's more efficient than checking it constantly.

avatar image wail13 Atenrev · Jul 29, 2019 at 09:08 PM 0
Share

@Atenrev So like this void OnTriggerEnter2D(Collider2D other) { if (health <= 0) { Destroy(gameObject); Scene$$anonymous$$anager.LoadScene(thisScene); } if (GetComponent().health <= 0) { score = score + 10; } if (GetComponent().health <= 0) { score = score + 5; } if (GetComponent().health <= 0) { score = score + 1; } text.text = score.ToString(); }

Show more comments

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

181 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 add one score every second to scoremanager c# 1 Answer

Highscore table C# HELP!!! 0 Answers

How do you add +1 to a score counter after a destroy script (C#)? 1 Answer

Increasing score based on speed 1 Answer

Strange (Pun2) RPC Problem. (With Error message that doesn't appear in a google search at all) 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