Tanks tutorial no damage on shell explosion

i followed tutorial but when but the shell does not give damage to player health. i tried accessing health by buttons but it works but shell damage not working the only code i change apart from tutorial is. particles.duration to particles.main.duration

using UnityEngine;

namespace Complete
{
public class ShellExplosion : MonoBehaviour
{
public LayerMask m_TankMask; // Used to filter what the explosion affects, this should be set to “Players”.
public ParticleSystem m_ExplosionParticles; // Reference to the particles that will play on explosion.
public AudioSource m_ExplosionAudio; // Reference to the audio that will play on explosion.
public float m_MaxDamage = 100f; // The amount of damage done if the explosion is centred on a tank.
public float m_ExplosionForce = 1000f; // The amount of force added to a tank at the centre of the explosion.
public float m_MaxLifeTime = 2f; // The time in seconds before the shell is removed.
public float m_ExplosionRadius = 5f; // The maximum distance away from the explosion tanks can be and are still affected.

private void Start ()
{
// If it isn’t destroyed by then, destroy the shell after it’s lifetime.
Destroy (gameObject, m_MaxLifeTime);
}

private void OnTriggerEnter (Collider other)
{

Collider[ ] colliders = Physics.OverlapSphere(transform.position, m_ExplosionRadius, m_TankMask);

for (int i = 0; i < colliders.Length; i++)
{

Rigidbody targetRigidbody = colliders .GetComponent ();
if (!targetRigidbody)
continue;

targetRigidbody.AddExplosionForce (m_ExplosionForce, transform.position, m_ExplosionRadius);
TankHealth targetHealth = targetRigidbody.GetComponent ();
if (!targetHealth)
continue;

float damage = CalculateDamage (targetRigidbody.position);
targetHealth.TakeDamage (damage);
}
m_ExplosionParticles.transform.parent = null;
m_ExplosionParticles.Play ();
m_ExplosionAudio.Play ();
Destroy (m_ExplosionParticles.gameObject, m_ExplosionParticles.main.duration);

}
private float CalculateDamage (Vector3 targetPosition)
{
Vector3 explosionToTarget = targetPosition - transform.position;
float explosionDistance = explosionToTarget.magnitude;
float relativeDistance = (m_ExplosionRadius - explosionDistance) / m_ExplosionRadius;
float damage = relativeDistance * m_MaxDamage;
damage = Mathf.Max (0f, damage);
return damage;
}

}

}
using UnityEngine;
using UnityEngine.UI;
public class TankHealth : MonoBehaviour
{
public float m_StartingHealth = 100f;
public Slider m_Slider;
public Image m_FillImage;
public Color m_FullHealthColor = Color.green;
public Color m_ZeroHealthColor = Color.red;
public GameObject m_ExplosionPrefab;

private AudioSource m_ExplosionAudio;
private ParticleSystem m_ExplosionParticles;
private float m_CurrentHealth;
private bool m_Dead;
private void Awake()
{
m_ExplosionParticles = Instantiate(m_ExplosionPrefab).GetComponent();
m_ExplosionAudio = m_ExplosionParticles.GetComponent();
m_ExplosionParticles.gameObject.SetActive(false);
}
private void OnEnable()
{
m_CurrentHealth = m_StartingHealth;
m_Dead = false;
SetHealthUI();
}

public void TakeDamage(float amount)
{
// Adjust the tank’s current health, update the UI based on the new health and check whether or not the tank is dead.
m_CurrentHealth -= amount;
SetHealthUI ();
if (m_CurrentHealth <= 0f && !m_Dead) {
OnDeath ();
}
}
private void SetHealthUI()
{
// Adjust the value and colour of the slider.
m_Slider.value = m_CurrentHealth;
m_FillImage.color = Color.Lerp (m_ZeroHealthColor, m_FullHealthColor, m_CurrentHealth / m_StartingHealth);
}
private void OnDeath()
{
// Play the effects for the death of the tank and deactivate it.
m_Dead = true;
m_ExplosionParticles.transform.position = transform.position;
m_ExplosionParticles.gameObject.SetActive (true);
m_ExplosionParticles.Play ();
m_ExplosionAudio.Play ();
gameObject.SetActive (false);
}
}

using UnityEngine;

namespace Complete
{
public class ShellExplosion : MonoBehaviour
{
public LayerMask m_TankMask; // Used to filter what the explosion affects, this should be set to "Players".
public ParticleSystem m_ExplosionParticles; // Reference to the particles that will play on explosion.
public AudioSource m_ExplosionAudio; // Reference to the audio that will play on explosion.
public float m_MaxDamage = 100f; // The amount of damage done if the explosion is centred on a tank.
public float m_ExplosionForce = 1000f; // The amount of force added to a tank at the centre of the explosion.
public float m_MaxLifeTime = 2f; // The time in seconds before the shell is removed.
public float m_ExplosionRadius = 5f; // The maximum distance away from the explosion tanks can be and are still affected.


private void Start ()
{
// If it isn't destroyed by then, destroy the shell after it's lifetime.
Destroy (gameObject, m_MaxLifeTime);
}


private void OnTriggerEnter (Collider other)
{

Collider[] colliders = Physics.OverlapSphere(transform.position, m_ExplosionRadius, m_TankMask);

for (int i = 0; i < colliders.Length; i++) 
{

Rigidbody targetRigidbody = colliders [I].GetComponent<Rigidbody> ();

if (!targetRigidbody)
continue;

targetRigidbody.AddExplosionForce (m_ExplosionForce, transform.position, m_ExplosionRadius);

TankHealth targetHealth = targetRigidbody.GetComponent<TankHealth> ();

if (!targetHealth)
continue;

float damage = CalculateDamage (targetRigidbody.position);
targetHealth.TakeDamage (damage);
}

m_ExplosionParticles.transform.parent = null;
m_ExplosionParticles.Play ();
m_ExplosionAudio.Play ();
Destroy (m_ExplosionParticles.gameObject, m_ExplosionParticles.main.duration);



}


private float CalculateDamage (Vector3 targetPosition)
{
Vector3 explosionToTarget = targetPosition - transform.position;

float explosionDistance = explosionToTarget.magnitude;
float relativeDistance = (m_ExplosionRadius - explosionDistance) / m_ExplosionRadius;
float damage = relativeDistance * m_MaxDamage;
damage = Mathf.Max (0f, damage);
return damage;


}

}

} 






using UnityEngine;
using UnityEngine.UI;

public class TankHealth : MonoBehaviour
{
public float m_StartingHealth = 100f; 
public Slider m_Slider; 
public Image m_FillImage; 
public Color m_FullHealthColor = Color.green; 
public Color m_ZeroHealthColor = Color.red; 
public GameObject m_ExplosionPrefab;


private AudioSource m_ExplosionAudio; 
private ParticleSystem m_ExplosionParticles; 
private float m_CurrentHealth; 
private bool m_Dead; 


private void Awake()
{
m_ExplosionParticles = Instantiate(m_ExplosionPrefab).GetComponent<ParticleSystem>();
m_ExplosionAudio = m_ExplosionParticles.GetComponent<AudioSource>();

m_ExplosionParticles.gameObject.SetActive(false);
}




private void OnEnable()
{
m_CurrentHealth = m_StartingHealth;
m_Dead = false;

SetHealthUI();
}


public void TakeDamage(float amount)
{
// Adjust the tank's current health, update the UI based on the new health and check whether or not the tank is dead.
m_CurrentHealth -= amount;
SetHealthUI ();
if (m_CurrentHealth <= 0f && !m_Dead) {
OnDeath ();
}
}


private void SetHealthUI()
{
// Adjust the value and colour of the slider.
m_Slider.value = m_CurrentHealth;
m_FillImage.color = Color.Lerp (m_ZeroHealthColor, m_FullHealthColor, m_CurrentHealth / m_StartingHealth);

}


private void OnDeath()
{
// Play the effects for the death of the tank and deactivate it.

m_Dead = true;
m_ExplosionParticles.transform.position = transform.position;
m_ExplosionParticles.gameObject.SetActive (true);

m_ExplosionParticles.Play ();

m_ExplosionAudio.Play ();

gameObject.SetActive (false);
}
}[/I]
[I]

I am running into the same issue. When the shell explodes no damage is shown by the UI and the tank isn’t effected by the explosion force.[/I]

I’ve never done that tutorial, but there are sections (for all Unity tutorials, I believe).
Here is the one for tanks: TANKS! Tutorial Q&A - Learn Content & Certification - Unity Discussions

Just looking it over…
Did you set the TankMask properly?
Does the target rigidbody come back null by any chance? or same with the tank health? (You can ‘print’ or ‘Debug.Log’ some places to check those things… except the mask you probably have to check that in the inspector).

Indentation added:
code with indent

using UnityEngine;
using UnityEngine.UI;

public class TankHealth : MonoBehaviour
{
    public float m_StartingHealth = 100f;
    public Slider m_Slider;
    public Image m_FillImage;
    public Color m_FullHealthColor = Color.green;
    public Color m_ZeroHealthColor = Color.red;
    public GameObject m_ExplosionPrefab;


    private AudioSource m_ExplosionAudio;
    private ParticleSystem m_ExplosionParticles;
    private float m_CurrentHealth;
    private bool m_Dead;


    private void Awake()
    {
        m_ExplosionParticles = Instantiate(m_ExplosionPrefab).GetComponent<ParticleSystem>();
        m_ExplosionAudio = m_ExplosionParticles.GetComponent<AudioSource>();

        m_ExplosionParticles.gameObject.SetActive(false);
    }




    private void OnEnable()
    {
        m_CurrentHealth = m_StartingHealth;
        m_Dead = false;

        SetHealthUI();
    }


    public void TakeDamage(float amount)
    {
        // Adjust the tank's current health, update the UI based on the new health and check whether or not the tank is dead.
        m_CurrentHealth -= amount;
        SetHealthUI();
        if (m_CurrentHealth <= 0f && !m_Dead)
        {
            OnDeath();
        }
    }


    private void SetHealthUI()
    {
        // Adjust the value and colour of the slider.
        m_Slider.value = m_CurrentHealth;
        m_FillImage.color = Color.Lerp(m_ZeroHealthColor, m_FullHealthColor, m_CurrentHealth / m_StartingHealth);

    }


    private void OnDeath()
    {
        // Play the effects for the death of the tank and deactivate it.

        m_Dead = true;
        m_ExplosionParticles.transform.position = transform.position;
        m_ExplosionParticles.gameObject.SetActive(true);

        m_ExplosionParticles.Play();

        m_ExplosionAudio.Play();

        gameObject.SetActive(false);
    }
}

Could be a variety of things. Like methos said, process of elimination. Put prints inside functions to ensure they’re getting called, eg print(colliders.Length); before the for loop in the OnTriggerEnter method of ShellExplosion to check if it’s actually hitting anything, in which case the problem is your layermasking.

Hey, so I was experiencing the same problem with my tanks not taking damage. (This is a year late but hey, the future’s on the line.)

All my code’s the same as yours; The code’s not the problem.

It’s the Layer that the Tank is set to. I didn’t set my Layer to the 9th row, which is ‘Players’.

This part is really forgettable but it makes all the difference.

They talk about how layers work here: TANKS! Unity Tutorial - Phase 2 of 8 - Tank Creation & Control 2:09 (

)