The error message in the title occurs intermittently when NetworkView.Find(id) is called. Obviously this only occurs when two or more players are connected.
This Gun script is enabled on a given client only for the local player GameObject it’s attached to.
public class Gun : MonoBehaviour
{
public GameObject shot;
...
public void Shoot()
{
if (Network.time > delay)
{
delay = (float)Network.time + rate;
Vector3 pos = transform.position + transform.forward * transform.localScale.z * 1f;
GameObject clone = Network.Instantiate(shot, pos, transform.rotation, 10) as GameObject;
networkView.RPC("InitializeShot", RPCMode.All, clone.networkView.viewID);
}
}
[RPC]
private void InitializeShot(NetworkViewID id)
{
NetworkView netView = NetworkView.Find(id);
if (netView != null) // shouldn't be null, but sometimes it is...
{
GameObject clone = netView.gameObject;
if (clone != null)
{
clone.rigidbody.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
clone.GetComponent<PG_Shot>().gun = this;
}
}
}
...
}
I’ve been banging my head over this for a while now. Any assistance is greatly appreciated.
edit: Disregard this answer. It’s still not working correctly consistently. I still need help.
Geh, it was just me being silly and not seeing what was in front of my face. All I needed was to make sure only I (the owner of the Gun) was instantiating shots. This required a networkView.isMine as so:
public void Shoot()
{
if (Network.time > delay && networkView.isMine)
{
For the sake of completeness, here is the destroy portion of my code (which I belatedly realized might be important):
public class Shot : MonoBehaviour
{
public Gun gun;
public float persist = 6f;
private float timeAtStart;
...
private void Start()
{
...
timeAtStart = (float)Network.time;
}
private void Update()
{
// persist for network functionality && this shot belongs to me (on the network)
if (Network.time > timeAtStart + persist && gun != null && gun.networkView.isMine)
{
Network.RemoveRPCs(networkView.viewID);
Network.Destroy(gameObject); // destroy for the server and all clients
}
}
private void OnTriggerEnter(Collider other)
{
...
if (gun != null && gun.networkView.isMine) // this shot belongs to me (on the network)
{
Network.RemoveRPCs(networkView.viewID);
Network.Destroy(gameObject); // destroy for the server and all clients
}
}
}