When a bullet hit an asteroid, the health bar decrease of 10 if the player is only local, so how Instantiate the healthbar on the server, this is my scripts :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class AsteroidController025 : NetworkBehaviour
{
[SerializeField]
private StatA asteroidHealth025;
private void Awake()
{
asteroidHealth025.Initialize();
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Bullet")
{
asteroidHealth025.CurrentVal -= 10;
Destroy(other.gameObject);
}
}
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (asteroidHealth025.CurrentVal <= 0)
{
Destroy(this.gameObject);
}
}
}
and
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
[Serializable]
public class StatA
{
[SerializeField]
private ABarScript aze;
[SerializeField]
private float maxVal;
[SerializeField]
private float currentVal;
public float CurrentVal
{
get
{
return currentVal;
}
set
{
this.currentVal = Mathf.Clamp(value, 0, MaxVal);
aze.Value = currentVal;
}
}
public float MaxVal
{
get
{
return maxVal;
}
set
{
this.maxVal = value;
aze.MaxValue = maxVal;
}
}
public void Initialize()
{
this.MaxVal = maxVal;
this.CurrentVal = currentVal;
}
}