Heya, i’m currently trying to make a health/bomb spawner for a unity school project, but am having some trouble with the netcoding. at the moment when i start and join the server, the spawner of health and bombs does spawn a orb ( that is either health or a bomb), the orbs itself have the tag Health and Bomb and the spawner itself has the tag Spawner. I am checking on the networked player with an ontriggerenter if we went through any of these tags (if its health or bomb we destory the orb, if its spawner we do all the necesary checks). But atm when i walk through the spawner and orbs, it doesn’t actually go fully through these checks ( atleast i think) and isn’t adding health or bombs, the orbs work perfectly fine, i’m just destroying them. Anyone able to help me or even give me a better way of doing it?
NETWORKED PLAYER FUNCTIONS INVOLVED:
public void AddBombs(int amountAdded)
{
RequestBombChangeServerRpc(amountAdded);
}
public void AddHealth(int amountAdded)
{
RequestHealthChangeServerRpc(health.Value + amountAdded);
}
private void RequestHealthChangeServerRpc(int newHealth)
{
health.Value = newHealth;
}
[Rpc(SendTo.Server)]
private void RequestBombChangeServerRpc(int newBomb)
{
bombs.Value += newBomb;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Health") || other.gameObject.CompareTag("Bomb"))
{
other.gameObject.GetComponent<NetworkDestroyer>().DestroyThis();
}
if(other.gameObject.CompareTag("Spawner"))
{
other.gameObject.GetComponent<NetworkedSpawner>().CheckIfSpawned(this.gameObject);
}
}
NETWORKEDSPAWNER SCRIPT:
private bool hasSpawned;
private bool bombSpawned;
private bool healthSpawned;
[SerializeField] private GameObject healingOrb;
[SerializeField] private GameObject bombOrb;
private void Start()
{
hasSpawned = false;
bombSpawned = false;
healthSpawned = false;
SpawnHealingOrBombRpc();
}
private IEnumerator WaitForRespawn()
{
Debug.Log("Waiting to spawn another one");
yield return new WaitForSeconds(10);
SpawnHealingOrBombRpc();
hasSpawned = true;
}
[Rpc(SendTo.Server)]
private void SpawnHealingOrBombRpc()
{
hasSpawned = true;
int random = UnityEngine.Random.Range(1,3 );
ActualSpawningRpc(random);
}
[Rpc(SendTo.Everyone)]
private void ActualSpawningRpc(int random)
{
Debug.Log("Spawned Another One");
if (random == 1)
{
Instantiate(healingOrb, transform.position + new Vector3(0, 0.5f, 0), quaternion.identity);
healthSpawned = true;
}
else if (random == 2)
{
Instantiate(bombOrb, transform.position + new Vector3(0, 0.5f, 0), quaternion.identity);
bombSpawned = true;
}
}
public void CheckIfSpawned(GameObject player)
{
if (hasSpawned)
{
if (bombSpawned)
{
gameObject.GetComponent<NetworkedPlayer>().AddBombs(1);
hasSpawned = false;
bombSpawned = false;
healthSpawned = false;
StartCoroutine(WaitForRespawn());
Debug.Log("Player has picked up bomb");
}
else if (healthSpawned)
{
gameObject.GetComponent<NetworkedPlayer>().AddHealth(50);
hasSpawned = false;
bombSpawned = false;
healthSpawned = false;
StartCoroutine(WaitForRespawn());
Debug.Log("Player has picked up health");
}
}
}