简体   繁体   中英

Tags not working in Unity C#

I wrote the following code to damage my player, however it is not working. I have checked as many times as I can for coding errors, and I know my problem is the tags. Using the Debug.log feature, I was able to confirm the problem was not with my hitboxes, and the tags are properly set up. The same system worked in reverse for my player projectiles to damage enemies. However, it is not working when set up with my player.

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

public class DamageToPlayer : MonoBehaviour {

public float damageAmount;


public void OnCollisionEnter(Collision col)
 {
    if(col.gameObject.tag == "Player")
    {

        Debug.Log("nearlyThere");
        PlayerHealth ifPlayer = col.gameObject.GetComponent<PlayerHealth>();
        if(ifPlayer != null)
        {
            ifPlayer.PlayerDamage(damageAmount);

            Debug.Log("TOUCH");
        }

    }
}

This is the enemy code.

}



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

public class PlayerHealth : MonoBehaviour {
public float PlayerStartHealth;

public float playerHealth;
public Text text;

public void Start () {
    playerHealth = PlayerStartHealth;
}


 public void PlayerDamage(float pAmount)
{
    playerHealth -= pAmount;
    if (playerHealth <= 0)
    {
        EndLevel();
    }
}

public void EndLevel()
{
    Debug.Log("u died m8");
}
public void FixedUpdate()
{
    text.text = playerHealth.ToString();

}

Here is more info: Neither of the Debugs are coming up on the console. That is how I know the problem.

}

this is the player health code itself. I have no idea why this is not working.

Place the following code as the first line of your OnCollisionEnter function:

Debug.Log("Collision between " + gameObject.name + " and " + 
    col.gameObject.name + " with tag " + col.gameObject.tag);

This will tell you if you have applied the DamageToPlayer script to your projectile correctly and if you have tagged the Player correctly and if all of your colliders are indeed setup correctly.

If you do not get this message at all it could be that the objects are travelling too fast for the collision to be detected. To prevent this, set the CollisionDetectionMode of your RigidBodies to Continuous or ContinuousDynamic. But keep in mind this comes at a performance cost.

Debug.Log($"gameObject.name: {col.gameObject.name}  gameObject.tag: {col.gameObject.tag}");

Try this code at the first line of DamageToPlayer.OnCollisionEnter() .

And then, check the other object's name and tag. You can check the collision is working correctly with this.

If collision is working but tag is different what you expected, change object tag. When the log not working, you should check both gameobject has Collider and one of them has RigidBody component.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM