- Home /
How to add different score when player hit different enemy,how to add different scores while player hitting different type of enemies
,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();
}
}
}
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
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
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.
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
Follow this Question
Related Questions
Scoreboard not updating 2 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