Variable and OnTriggerEnter

Hi, I’m a newbie on unity and I have a problem with the onColliderTrigger method.
To make it simple I have a method that instantiates a prefab and once it is instantiated I set it up with several properties.

    public void HandleProjectile()
    {
        GameObject _projectile = Instantiate(projectilePrefab, new Vector3(0, 0, 0), transform.rotation);

        ProjectileScript _projectileScript = _projectile.AddComponent<ProjectileScript>();
        _projectileScript.projectileStartingPoint = transform.position;
        _projectileScript.projectileDir = transform.forward.normalized;
        _projectileScript.ability = ability;
    }

the script attached to the prefab only control the movement and the collisions other object.

public class ProjectileScript : MonoBehaviour
{
    [HideInInspector]
    public Vector3 projectileStartingPoint;
    [HideInInspector]
    public Vector3 projectileDir;
    [HideInInspector]
    public Ability ability;

    void Start()
    {
        transform.position = projectileStartingPoint;
    }

    void Update()
    {
        transform.position += projectileDir * ability.ProjectileVelicity * Time.deltaTime;
        if (Vector3.Distance(projectileStartingPoint, transform.position) >= ability.baseRange)
        {
            Destroy(gameObject);
        }
    }

    void OnTriggerEnter(Collider hitCollider)
    {
        if (hitCollider.gameObject.layer == LayerMask.NameToLayer("Enemies"))
        {
            hitCollider.gameObject.SendMessage("AddDamage", ability.baseDamage);
            Destroy(gameObject);
        }
    }
}

the script works as expected for the movement. But when a collision with another object occurs, all my variables are set to null only in the OnTriggerEnter and not elsewhere, and therefore I can no longer access to ability.baseDamage in my SendMessage method. I don’t understand what I’m doing wrong.

Start by sprinkling in LOTS of Debug.Log() statement all over your code to identify:

  • what code is even running (any of it?)
  • what’s the name of the GameObject this script is on? (output the name field)
  • what are the values of the variables in question?

etc. etc. etc.

Keep adding Debug.Log() statements until you understand exactly why the program is doing what it is.