Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 $$anonymous$$ · Feb 02, 2021 at 07:18 AM · scripting problemscripting beginnerscore systemscoreboardscores

How to add different score when player hit different enemy,how to add different scores while player hitting different type of enemies

alt text

,using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Enemy : MonoBehaviour { [SerializeField] private float moveSpeed = 5f;

 // Update is called once per frame
 void Update()
 {
     var xPos = this.transform.position.x;
     xPos -= 0.75f * Time.deltaTime * moveSpeed;
     this.transform.position = new Vector2(xPos, this.transform.position.y);
 }

 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.transform.tag.Equals("EnemyDestroyer"))
     {
         GamePlayManager.instance.MissCount();
         Destroy(gameObject);
     }

     if (collision.transform.tag.Equals("Bullet"))
     {
         Destroy(gameObject);
         GamePlayManager.instance.ScoreCount();
     }
 }

}

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

public class Enemy : MonoBehaviour { [SerializeField] private float moveSpeed = 5f;

 // Update is called once per frame
 void Update()
 {
     var xPos = this.transform.position.x;
     xPos -= 0.75f * Time.deltaTime * moveSpeed;
     this.transform.position = new Vector2(xPos, this.transform.position.y);
 }

 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.transform.tag.Equals("EnemyDestroyer"))
     {
         GamePlayManager.instance.MissCount();
         Destroy(gameObject);
     }

     if (collision.transform.tag.Equals("Bullet"))
     {
         Destroy(gameObject);
         GamePlayManager.instance.ScoreCount();
     }
 }

}

screenshot-2021-02-02-124437.png (54.5 kB)
screenshot-2021-02-02-124354.png (82.5 kB)
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 Llama_w_2Ls · Feb 02, 2021 at 07:39 AM

Well since you are using singeltons, you could use a public int for your score in the GamePlayManager, and a switch statement to handle scores. For example:

 public static GamePlayManager instance;
 public int Score;

And in your collision script:

 void OnTriggerEnter2D(Collider2D collision)
 {
      switch(collision.gameObject.tag)
      {
           case "EnemyDestroyer":
                GamePlayManager.instance.Score += 2;
                // Do other stuff here too
                break;
 
           case "SuperEnemy":
                 GamePlayManager.instance.Score += 5;
                 break;
      }
 }

Hope that helps @kumar_reddy

Comment
Add comment · Show 3 · 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 $$anonymous$$ · Feb 02, 2021 at 09:41 AM 0
Share

switch case is not activating and score is not calling but it is not showing any error @Llama_w_2Ls

[1]: /storage/temp/175236-screenshot-2021-02-02-124437.png

screenshot-2021-02-02-150907.png (70.5 kB)
screenshot-2021-02-02-124437.png (47.3 kB)
avatar image Llama_w_2Ls $$anonymous$$ · Feb 02, 2021 at 10:54 AM 0
Share

You don't need to nest the switch statement in any if loops. The whole point of the if statement is to handle all different types of tags. When you put it in the if loop, it only runs if the tag is 'bullet', which defeats the purpose. You can implement it directly as I did, and add 'bullet' as a separate case in the switch statement.

avatar image AbandonedCrypt $$anonymous$$ · Feb 02, 2021 at 10:55 AM 0
Share

Use Debug.Log() to find out where your code stops executing and then work form there. With tags it's often the problem of not tagging correctly, typos, capitalization. For your case you should try using gameObject.tag, idek what transform.tag is and how that doesnt result in an error. Also use CompareTag() where possible.


edit:// i see transform.tag seems to cache gameObject.tag, so i could see your problem being elsewhere:

You compare to tag "Bullet" and only if true go into the switch. You can't have more than one tag though, so if something is tagged "Bullet" it already can't be tagged anything else.

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

211 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Scoreboard not updating 2 Answers

how to creat and scoring and high score system for an endless runner,how do I display my score on a game over scene and also create and high score system for the same ? 0 Answers

How to make a scoreboard? 0 Answers

Local Party Game Score Transfer 1 Answer

Can't increase my score after OnTriggerEnter occurs. 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