I have tried to make a script which syncs gun sounds so when player A shoots, player B hears the gun fire from player A's audio source. I have tried to use RPC's but got nowhere. If someone could write a quick script which achieves this or link me to a script that would be brilliant. FYI, I'm using uNet.
You'll be hard pressed to get someone to write a script for you around here...even one so simple. However, you're on the right track. You can use a ClientRPC, AND a Command. The player shooting would send a Command to the host/server that would then send a ClientRPC out to all clients to make that player's PlayerObject execute the sound.
I've written this but I get the error ' Uncaught exception in async net callback: Object reference not set to an instance of an object UnityEditor.AsyncHTTPClientone(State, Int32)' and I cannot hear anything. Any ideas? Code (CSharp): using UnityEngine; using UnityEngine.Networking; public class PlayerShoot : NetworkBehaviour { private const string PLAYER_TAG = "Player"; public PlayerWeapon weapon; public GameObject hitPar; public GameObject hitPar_1; private AudioSource audioSource; public AudioClip hurtSound; [SyncVar] public AudioClip shootSound; [SerializeField] private Camera cam; [SerializeField] private LayerMask mask; void Awake () { audioSource = GetComponent<AudioSource> (); } void Start () { if (cam == null) { Debug.LogError("PlayerShoot: No camera referenced!"); this.enabled = false; } } void Update () { if (Input.GetButtonDown("Fire1")) { Shoot(); PlaySound (); } } [Client] void Shoot () { RaycastHit _hit; if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, weapon.range, mask) ) { if (_hit.collider.tag == PLAYER_TAG) { CmdPlayerShot (_hit.collider.name, weapon.damage); GameObject par_clone = Instantiate (hitPar_1, _hit.point, Quaternion.LookRotation (_hit.normal)) as GameObject; Destroy (par_clone, 0.5f);; audioSource.clip = hurtSound; audioSource.Play (); } else { GameObject par_clone = Instantiate (hitPar, _hit.point, Quaternion.LookRotation (_hit.normal)) as GameObject; Destroy (par_clone, 0.5f);; } } } [Command] void CmdPlayerShot (string _playerID, int _damage) { Debug.Log(_playerID + " has been shot."); Player _player = GameManager.GetPlayer(_playerID); _player.RpcTakeDamage(_damage); } public void PlaySound() { if(!isServer) { return; RpcSound (); } } [ClientRpc] void RpcSound() { if(!isLocalPlayer) { audioSource.clip = shootSound; audioSource.Play (); } } }
Not sure if this will help you, but look at this thead > http://forum.unity3d.com/threads/4-...tate-int32-exception-from-asset-store.220517/
lol Sorry...I'm not familiar with that error you got so I was hoping you could find an answer to the error in that thread.