[SOLVED] Collisions not detecting

I’m having issues with OnTriggerEnter on the projectile. It isn’t getting called. However, if I remove the [ServerCallBack] then it is called (tested with debug.log) but not doing anything - presumably as the client is detecting the collision and not being able to do anything about it.


using Mirror;
using UnityEngine;

public class UnitProjectile : NetworkBehaviour
{
    [SerializeField] private Rigidbody myRigidBody = null;
    [SerializeField] private int damageToDeal = 20;
    [SerializeField] private float launchForce = 10f;
    [SerializeField] private float destroyAfterSeconds = 5f;

    private void Start()
    {
        myRigidBody.velocity = transform.forward * launchForce;
    }

    public override void OnStartServer()
    {
        Invoke(nameof(DestroySelf), destroyAfterSeconds);
    }

    [ServerCallback]
    private void OnTriggerEnter(Collider other)
    {
        if (other.TryGetComponent<NetworkIdentity>(out NetworkIdentity networkIdentity))
        {
            if (networkIdentity.connectionToServer == connectionToServer) { return; }
        }

        if (other.TryGetComponent<Health>(out Health health))
        {
            health.DealDamage(damageToDeal);
        }

        DestroySelf();
    }

    [Server]
    private void DestroySelf()
    {
        NetworkServer.Destroy(gameObject);
    }
}
using Mirror;
using System;
using UnityEngine;

public class Health : NetworkBehaviour
{
    [SerializeField] private int maxHealth = 100;

    [SyncVar] private int currentHealth;

    public event Action ServerOnDie;

    #region Server

    public override void OnStartServer()
    {
        currentHealth = maxHealth;
    }

    [Server]
    public void DealDamage(int damageAmount)
    {
        if (currentHealth == 0) { return; }

        currentHealth = Mathf.Max(currentHealth - damageAmount, 0);

        if (currentHealth != 0) { return; }

        ServerOnDie?.Invoke();

        Debug.Log("Unit died");
    }

    #endregion

    #region Client



    #endregion
}

Hi Clive,

Have you checked your code against the code in the resources as this would be the best bet to go through this with a fine tooth comb.

You do have a null reference there in the console so i wonder which line of code that refers to.

It may help to track the problem to confirm the line in which the null reference is referring to.

Please do let us know or if you solve the issue

OK. Not sure why my debug.log as the first line of OnTriggerEnter didn’t work, but the solution to this is to NOT BE AN IDIOT.

It kinda helps if I change networkIdentity.connectionToServer to networkIdentity. connectionToClient…

You are not an idiot :slight_smile:
We have all done it at some point and i have probably more times than most.

Glad you spotted the error and got this solved because i missed it too!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms