I have a grenade Prefab in my Resources folder for a online game I’m working on…
I want this grenade to instantiate on all the clients. I get an error message saying this:
“Illegal view ID:0 method: Explode GO:Grenade”
On OnEnable() I try to execute the RPC “Explode” on all client, and I guess that’s what throws the error. What am I doing wrong? This script is attached to the grenade Prefab…
using UnityEngine;
using System.Collections;
public class GrenadeEffect : MonoBehaviour {
public enum GrenadeType {
Explosive,
Flashbang,
Smoke
};
[Header("Audio")]
public AudioSource explosionSource;
public AudioClip closeExpSound;
public AudioClip farExpSound;
[Space(5)]
public float farDistance = 25f;
[Header("Grenade Settings")]
public GrenadeType grenadeType = GrenadeType.Explosive;
public GameObject airExplosion = null;
public Light grenadeLight;
[Space(5)]
public float explosionForce = 15f;
public float radius = 20f;
[Range(10f, 80f)] public float damage = 45f;
// Private variables
private GameObject particleEffect;
private GameObject player;
private PhotonView photonView;
void OnEnable() {
photonView = this.GetComponent<PhotonView> ();
player = GameObject.FindGameObjectWithTag ("Player").gameObject;
grenadeLight.range = radius;
photonView.RPC ("Explode", PhotonTargets.All);
}
[PunRPC]
void Explode() {
this.GetComponent<AudioSource> ().Play ();
float distance = Vector3.Distance (this.transform.position, player.transform.position);
if (distance <= farDistance) {
this.explosionSource.PlayOneShot (closeExpSound);
} else if (distance > farDistance) {
this.explosionSource.PlayOneShot (farExpSound);
}
particleEffect = Instantiate (airExplosion, this.transform.position, Quaternion.identity) as GameObject;
particleEffect.transform.SetParent (this.transform.root);
Collider[] objectsInRange = Physics.OverlapSphere (this.transform.position, radius);
foreach (Collider col in objectsInRange) {
// Add explosion force to rigidbodys in range of the grenade explosion
Rigidbody rb = col.GetComponent<Rigidbody> ();
if (rb != null)
rb.AddExplosionForce (explosionForce, this.transform.position, radius, 3f);
// Add damage to players within the radius of the grenade.
if (col.GetComponent<PlayerDamageManager> ()) {
// Calculate damage (depends on distance from grenade)
float proximity = (this.transform.position - col.transform.position).magnitude;
float effect = 1 - (proximity / radius);
effect = Mathf.Clamp (effect, 0f, 1f);
// Apply damage
col.gameObject.GetComponent<PlayerDamageManager> ().GetDamage ((damage * effect), "Grenade", this.transform.position);
}
}
Destroy (grenadeLight.gameObject, closeExpSound.length / 2);
Destroy (this.gameObject, closeExpSound.length);
}
}
The Grenade Prefab has PhotonView Component…