Syncing Variables

Options
I am currently making an FPS game where the game starts and only one person on the server has a bullet. Once he shoots, a variable "players" is randomized and it determines who gets the bullet. The issue is that the variable is different for every player. I set the Fire void as a [PunRPC] but it still doesn't work.

Code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class PlayerShoot : MonoBehaviour {

public float damage = 100f;
FXControl fxControl;
public bool hasBullet = false;
WeaponData weaponData;

void Start(){
fxControl = GameObject.FindObjectOfType ();
weaponData = gameObject.GetComponentInChildren ();
GenerateAmmo();
}
void Awake(){
GameObject.Find ("Var").GetComponent().players = Random.Range (1, 3);
GameObject.Find ("Var").GetComponent().PlayerCount = GameObject.Find ("Var").GetComponent().PlayerCount + 1;
}
// Update is called once per frame
[PunRPC]
void Update () {
GameObject.Find ("Text").GetComponent ().text = "Player Count = " + GameObject.Find ("Var").GetComponent().PlayerCount;
GameObject.Find ("Text2").GetComponent ().text = "Players = " + GameObject.Find ("Var").GetComponent().players;
/*if (PhotonNetwork.player.name == ("AMMOGOD")){
hasBullet = true;
}*/
if (Input.GetButtonDown ("Fire1")) {
//Shoot
Fire ();
}
if (GameObject.Find ("Var").GetComponent().players == 1 && this.GetComponentInChildren().material.color == Color.blue){
this.hasBullet = true;
}
if (GameObject.Find ("Var").GetComponent().players == 2 && this.GetComponentInChildren().material.color == Color.red){
this.hasBullet = true;
}
}
[PunRPC]
void Fire(){
if (hasBullet == false) {
return;
}
hasBullet = false;
GameObject.Find ("Var").GetComponent().players = Random.Range (1, 3);
Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
Transform hitTransform;
Vector3 hitPoint;

hitTransform = FindClosestHitObject (ray, out hitPoint);

if (hitTransform != null) {
Debug.Log ("We shot: " + hitTransform.name);
Health h = hitTransform.GetComponent();

while (h == null && hitTransform.parent){
hitTransform = hitTransform.parent;
h = hitTransform.GetComponent();
}

if(h != null){
h.GetComponent().RPC("TakeDamage", PhotonTargets.AllBuffered, damage);
//h.TakeDamage(damage);
}
if (fxControl != null){
DoGunFX(hitPoint);
}else{
if (fxControl != null){
hitPoint = Camera.main.transform.position + (Camera.main.transform.forward * 100f);
DoGunFX(hitPoint);
}
}
}
}
void DoGunFX(Vector3 hitPoint){
fxControl.GetComponent().RPC ("BulletFX", PhotonTargets.All, weaponData.transform.position, hitPoint);
}

Transform FindClosestHitObject (Ray ray, out Vector3 hitPoint){
RaycastHit[] hits = Physics.RaycastAll (ray);

Transform closestHit = null;
float distance = 0;
hitPoint = Vector3.zero;

foreach (RaycastHit hit in hits) {
if(hit.transform != this.transform && (closestHit == null || hit.distance < distance)){
// We hit something that is a not us orthe first thing we hit or is at least closer than previous closest thing
closestHit = hit.transform;
distance = hit.distance;
hitPoint = hit.point;
}
}
//closestHit is not either still null or contains the closest thing
return closestHit;
}
void GenerateAmmo(){
//players = 1;
}
}

Answers

  • Tobias
    Options
    We can't check a lot of code here, sorry. You need to let us know more exactly where things fail.
    You can use Debug.Log() quite easily to find out if the RPCs get fired and you could log parameters, etc. Use that to find out what's missing exactly.

    Did you see this post about general issues with RPCs?
    http://forum.photonengine.com/discussion/6317/punrpc-help#latest

    So far, nobody replied that he couldn't resolve the issues after checking out those tips.