Hello,
I need your help for this problem:
When i attack with a triggered collider, im checking if the team of the attacker is not same as the victim, and if not the attack deal damage (check script under)
It works for a client that attack server, but when the victim is a client the int team(HitedTeam) of victim doesn’t apply and i got an error
Knife script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DamageKunaiHit : MonoBehaviour
{
public GameObject player;
private void OnTriggerEnter(Collider other)
{
GameObject hit = other.gameObject;
MinatoController_Net knock4 = hit.GetComponent<MinatoController_Net>();
NewHealth health = hit.GetComponent<NewHealth>();
Vector3 hitDirection = other.transform.position - player.transform.position;
hitDirection = hitDirection.normalized;
int hitTeam = player.GetComponent<PlayerManageScript>().team;
int hitedTeam = hit.GetComponent<PlayerManageScript>().team;
if (hit.GetComponent<MinatoController_Net>() != null)
{
if (hitTeam != hitedTeam)
{
player.GetComponent<PlayerManageScript>().ComboInt += 1;
player.GetComponent<PlayerManageScript>().DamageInt += 10;
health.TakeDamage(10, hitDirection);
knock4.KnockbackShort(hitDirection);
}
}
}
}
PlayerTeam Script1:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class PlayerManageScript : NetworkBehaviour
{
[SyncVar]public int team;
void Update () {
if (gameObject.GetComponent<MinatoController_Net>() != null)
{
team = gameObject.GetComponent<MinatoController_Net>().team;
}
PlayerTeamScript2:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Events;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class MinatoController_Net : NetworkBehaviour
{
[SyncVar]public int team;
void Update()
{
if (team <= 0)
{
if (Input.GetButtonDown("Team1"))
{
team = 1;
}
if (Input.GetButtonDown("Team2"))
{
team = 2;
}
NullReferenceException: Object reference not set to an instance of an object
DamageKunaiHit.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/DamageKunaiHit.cs:17)
I really need to fix that, can you help me please?
Hi,
As you can see in line 16 the playermanagerscript is working, and it’s the same prefab for line 17, i also checked and they both have it. Note that it doesn’t work only for client, but when you hit the server it works…
I really don’t understand, looks like network error but …
You can debug the code or add a debug log to the code, just to be sure:
Because the error messages states that the error happens at line 17, unless the code above is missing some code and the lines are different? And I don’t see anything else at line 17 which could cause a null value other than the GetComponent return value
Change line 17 int hitedTeam = hit.GetComponent().team;
to:
var hitedPlayerManageScript = hit.GetComponent<PlayerManageScript>();
if(hitedPlayerManageScript == null)
{
Debug.Log("Hited PlayerManageScript is null");
}
int hitedTeam = hitedPlayerManageScript.team
Oh, the debug appear so there is something wrong with the GetCompo, but as you can see in screenshots all player got PlayerManagerScript.
Note 2 things: Again it doesn’t work only when client receive damage, also when a client join i got 2 prefabs who got spawned i don’t know why, i don’t think that can be the problem but just saying
Could it be that the attack somehow collides with something else, which isn’t the player?
Try to add a additional Debug.Log line to print the hit object name.
Otherwise im not sure what could cause the problem.
Oh, the knife hit 2 things, the player and the terrain, could the collider with terrain create a problem with damage dealing to player?
If not the problem is somewhere else, the client doesn’t receive damage
If it hits the terrain the collider code would try to get the PlayerManageScript component of the terrain, which would return null. If that’s the cause you could add a return statement inside the if where you check if the hit PlayerManageScript is null to exit/ignore the damge code for any object which doesn’t have the player script.
I changed the hitbox, the doesn’t hit terrain anymore and no more console error, but still no damage, im trying things right now about team value and combo/damage line, maybe some of these cause the problem, i’ll post what i found in the afternoon
Anyway you really helped me until now, thank you very much