- Home /
Multiplayer: I have an OnTrigger script that isn't correctly responding. Spawning and Destroying GameObjects.
Hello! This is my first Networking game on Unity, and I'm having issues with spawning/destroying items. In short, this is what my script is trying to do. The script is attached to a Game Object with a Box Collider. Is Trigger checked on the Box Collider. When a player enters the trigger, text is enabled and the player is essentially asked to pick up the weapon. OnTriggerStay, if the player presses the "F" key (Action) then the prefab on the ground gets destroyed, and another prefab spawns to the players equipZone (hands). If the player is already holding a weapon, they can either "swap" weapons, or pickup the ammo from the weapon location. OnTriggerExit, the display text is disabled, and that's pretty much it.
As the code sits, I made 3 of the functions have the [Command] attribute. WITH the command attribute, client can't interact with the weapons. Error message pops the following: Trying to send command for object without authority.
So, there's 2 prefabs for each weapon. A ground prefab, that pretty much just holds a mesh, and an actionable prefab that equips to the player hands and has particle effects and what not. The GROUND prefab currently has Server Authority on the Network ID component enabled. The ACTIONABLE prefab has no authorities set. Enabling Server Authority on my GROUND prefabs allows my initial groundspawn to work. Otherwise, it spawns 2 prefabs on the ground.
On initial pickup: Server: As of right now, the server sees everything on it's side correctly. However, if the client tries to "pick up" a weapon, the server does not see the prefab on the ground get destroyed and doesn't see the gun snap to the client's equipzone (hands).
Client: Same vice versa, if server "picks up" a weapon, client does not see that prefab on the ground get destroyed, BUT the client can see the weapon snap to the server's hands.
For weapon swap: Server: Again, server sees what it's doing perfectly fine. BUT If client interacts, then no change is seen on server side.
Client: Client can see the prefab change for the server. (Server had an AK-47, now has an M416)
Here's the code! I apologize in advance if it's difficult to read.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Networking;
public class WeaponSpawnLocation : NetworkBehaviour {
public WeaponDatabase weaponDB;
public Text displayText;
[SyncVar(hook = "UpdateWeaponIndex")]
private int weaponIndex;
private GameObject weaponPrefabToBeDestroyed, weaponToDrop;
private Weapon tempWeaponVariable, initialWeapon;
private Transform equipZone;
private bool weaponToGround = false;
// Use this for initialization
void Start()
{
if (isServer) OnWeaponSpawnStart();
if (!isServer) UpdateWeaponIndex(weaponIndex);
SpawnGameObjectToGround(initialWeapon.weaponPickupPrefab);
}
private void OnTriggerEnter(Collider other)
{
if(isLocalPlayer)
{
Debug.Log("Local player entered Trigger!");
}
if (other.tag == "Player")
{
displayText.enabled = true;
displayText.text = "Press F to Equip " + initialWeapon.weaponName;
equipZone = other.GetComponent<Gun>().firingFacingDirection;
}
}
private void OnTriggerStay(Collider other)
{
if (other.tag != ("Player")) return;
// If you have the same ammo type, display remaining ammo of said type
if (initialWeapon.ammoType == other.GetComponent<Gun>().ammoType)
{
displayText.text = initialWeapon.ammoInReserves + initialWeapon.ammoInMagazine + " rounds of " + initialWeapon.ammoType;
}
// If ammo has been looted, read "No Ammo"
else if (initialWeapon.ammoType == other.GetComponent<Gun>().ammoType && (initialWeapon.ammoInMagazine + initialWeapon.ammoInReserves) == 0)
{
displayText.text = "No ammo";
}
// If weapon names don't match, state a "Swap" message
else if (initialWeapon.weaponName != other.GetComponent<Gun>().weaponName && CheckForWeapon.weaponEquipped)
{
displayText.text = "Swap " + other.GetComponent<Gun>().weaponName + " for " + initialWeapon.weaponName;
}
// If none of the above and player is empty handed, state a "Pickup" message
else
{
displayText.text = "Press F to Equip " + initialWeapon.weaponName;
}
// Execute the following when the F key is pressed
if (Input.GetButtonDown("Action"))
{
// If no weapon is equipped, pickup weapon and destroy the spawned original prefab
if (!CheckForWeapon.weaponEquipped)
{
CmdSpawnGameObjectToEquipZone(initialWeapon.weaponPrefab);
other.GetComponent<Gun>().weaponStats
= initialWeapon;
CmdDestroyObject(weaponPrefabToBeDestroyed);
displayText.enabled = false;
CmdDestroyObject(gameObject);
}
else
{
// Pick up ammo from location
if (initialWeapon.ammoType == other.GetComponent<Gun>().ammoType)
{
other.GetComponent<Gun>().ammoInReserves
+= initialWeapon.ammoInReserves;
other.GetComponent<Gun>().ammoInReserves
+= initialWeapon.ammoInMagazine;
initialWeapon.ammoInReserves -= initialWeapon.ammoInReserves;
initialWeapon.ammoInMagazine -= initialWeapon.ammoInMagazine;
//ammoPickedUp = true;
}
// Swap weapons
else if (initialWeapon.weaponName != other.GetComponent<Gun>().weaponName)
{
weaponToGround = true;
CmdSpawnGameObjectToGround(other.GetComponent<Gun>().weaponPickUpPrefab);
CmdDestroyObject(weaponPrefabToBeDestroyed);
weaponPrefabToBeDestroyed = weaponToDrop;
CmdDestroyObject(equipZone.GetChild(0).gameObject);
tempWeaponVariable = other.GetComponent<Gun>().weaponStats;
// Next two lines ensure the weapon that is dropped contains the ammo used/reserved
tempWeaponVariable.ammoInReserves = other.GetComponent<Gun>().ammoInReserves;
tempWeaponVariable.ammoInMagazine = other.GetComponent<Gun>().ammoInMagazine;
other.GetComponent<Gun>().weaponStats
= initialWeapon;
other.GetComponent<Gun>().weaponStatsChecked
= false;
CmdSpawnGameObjectToEquipZone(initialWeapon.weaponPrefab);
initialWeapon = tempWeaponVariable;
}
}
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
displayText.enabled = false;
equipZone = null;
}
}
void SpawnGameObjectToGround(GameObject objectToSpawn)
{
GameObject instance = Instantiate(objectToSpawn, this.gameObject.transform.position,
Quaternion.Euler(0, Random.Range(-180, 180), 90));
if (isServer)
{
NetworkServer.Spawn(instance);
}
if (weaponToGround)
{
weaponToDrop = instance;
}
else
{
weaponPrefabToBeDestroyed = instance;
}
}
[Command]
void CmdSpawnGameObjectToGround(GameObject objectToSpawn)
{
GameObject instance = Instantiate(objectToSpawn, this.gameObject.transform.position,
Quaternion.Euler(0, Random.Range(-180, 180), 90));
if (isServer)
{
NetworkServer.Spawn(instance);
}
//RpcSpawnGameObjectToGround(instance);
if (weaponToGround)
{
weaponToDrop = instance;
}
else
{
weaponPrefabToBeDestroyed = instance;
}
}
[Command]
void CmdSpawnGameObjectToEquipZone(GameObject objectToSpawn)
{
GameObject instance = Instantiate(objectToSpawn, equipZone);
if (isServer)
{
NetworkServer.Spawn(instance);
}
}
[Command]
void CmdDestroyObject(GameObject objectToDestroy)
{
Destroy(objectToDestroy);
NetworkServer.Destroy(objectToDestroy);
}
void OnWeaponSpawnStart()
{
UpdateWeaponIndex(Random.Range(0, weaponDB.weapons.Count));
}
void UpdateWeaponIndex(int _weaponIndex)
{
weaponIndex = _weaponIndex;
initialWeapon = new Weapon(weaponDB.weapons[weaponIndex].weaponID, weaponDB.weapons[weaponIndex].weaponPrefab,
weaponDB.weapons[weaponIndex].weaponPickupPrefab, weaponDB.weapons[weaponIndex].weaponName,
weaponDB.weapons[weaponIndex].weaponPickupText, weaponDB.weapons[weaponIndex].weaponDamage,
weaponDB.weapons[weaponIndex].weaponRecoil, weaponDB.weapons[weaponIndex].impactForce,
weaponDB.weapons[weaponIndex].fireRate, weaponDB.weapons[weaponIndex].magazineCapacity,
weaponDB.weapons[weaponIndex].ammoInMagazine, weaponDB.weapons[weaponIndex].ammoInReserves,
weaponDB.weapons[weaponIndex].ammoType);
} }