UPDATE this is not the main problem I had. check out this question
I've made a little multiplayer game in unity, and I want to know, when the opponent player is dead (bool oppdead).
If I run my code, and the opponent player dies, I do get the Log "opp player is dead", but my onGUI isnt beeing executed. Have I done something wrong? all my other bool's work perfectly..
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using GooglePlayGames.BasicApi.Multiplayer;
using UnityEngine.UI;
using System;
public class BirdMovementMP : MonoBehaviour, MPLobbyListener
{
public GameObject opponentPrefab;
public Rigidbody2D oppPlane;
private bool _multiplayerReady;
private string _myParticipantId;
public bool oppdead = false;
public float flapSpeed = 100f;
public float forwardSpeed = 1f;
bool didFlap = false;
Animator animator;
public bool dead = false;
float deathCooldown;
public Rigidbody2D plane;
public Button buttonImage;
public GUISkin replay;
public GUISkin home;
void SetupMultiplayerGame()
{
MultiplayerController.Instance.lobbyListener = this;
// 1
_myParticipantId = MultiplayerController.Instance.GetMyParticipantId();
// 2
List<Participant> allPlayers = MultiplayerController.Instance.GetAllPlayers();
_opponentScripts = new Dictionary<string, OpponentPlane>(allPlayers.Count - 1);
for (int i = 0; i < allPlayers.Count; i++)
{
string nextParticipantId = allPlayers[i].ParticipantId;
Debug.Log("Setting up car for " + nextParticipantId);
// 3
if (nextParticipantId == _myParticipantId)
{
// 4
//rigidbody2D.GetComponent<BirdMovementMP>().SetCarChoice(i + 1, true);
// myCar.transform.position = carStartPoint;
}
else
{
// 5
/* GameObject OpponentPlane = (Instantiate(opponentPrefab, carStartPoint, Quaternion.identity) as GameObject);
OpponentPlane opponentScript = OpponentPlane.GetComponent<OpponentPlane>();
opponentScript.SetCarNumber(i + 1);
// 6
_opponentScripts[nextParticipantId] = opponentScript;*/
}
}
// 7
_multiplayerReady = true;
}
public void UpdatePlane(string senderId, float x, float y, float z, bool death)
{
MultiplayerController.Instance.GetMyParticipantId();
// OpponentPlane opponent = _opponentScripts[senderId];
if (death)
{
Debug.Log("opp Player is dead");
oppdead = true;
}
if (opponentPrefab != null)
{
opponentPrefab.transform.position = new Vector3(x, y, 0);
opponentPrefab.transform.rotation = Quaternion.Euler(0, 0, z);
Debug.Log("setting opp pos new");
}
if (opponentPrefab == null)
{
// Debug.Log("oppo is gleich null");
opponentPrefab = GameObject.Find("Opponent");
opponentPrefab.transform.position = new Vector3(x, y, 0);
opponentPrefab.transform.rotation = Quaternion.Euler(0, 0, z);
}
}
void doMultiplayerUpdate()
{
MultiplayerController.Instance.SendMyUpdate(plane.transform.position.x,
plane.transform.position.y,
plane.transform.rotation.eulerAngles.z,
dead);
// Debug.Log("Im at position:" + plane.transform.position.x + "x" + plane.transform.position.x + "y");
}
// Use this for initialization
void Start()
{
if(opponentPrefab == null)
{
opponentPrefab = GameObject.Find("Opponent");
}
animator = transform.GetComponentInChildren<Animator>();
Time.timeScale = 0;
if (animator == null)
{
Debug.LogError("Didn't find animator!");
}
}
void OnGUI()
{
if (oppdead)
{
GUI.skin.label.fontSize = Screen.height / 20;
GUI.Label(new Rect(Screen.height / 2, Screen.height / 2, Screen.height / 2, Screen.height / 2), " other is deadddd ");
}
if (dead)
{
//Menu Button
GUI.skin = null;
GUI.skin = home;
if (GUI.Button(new Rect(10, Screen.height / 2, Screen.height / 4, Screen.height / 4), ""))
{
Application.LoadLevel("home");
}
}
}
// Do Graphic & Input updates here
void Update()
{
doMultiplayerUpdate();
if (dead)
{
deathCooldown -= Time.deltaTime;
}
else
{
if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
{
didFlap = true;
}
}
}
// Do physics engine updates here
void FixedUpdate()
{
if (dead)
return;
plane.AddForce(Vector2.right * forwardSpeed);
if (didFlap)
{
plane.AddForce(Vector2.up * flapSpeed);
animator.SetTrigger("DoFlap");
didFlap = false;
}
if (plane.velocity.y > 0)
{
transform.rotation = Quaternion.Euler(0, 0, 0);
}
else
{
float angle = Mathf.Lerp(0, -90, (-plane.velocity.y / 3f));
transform.rotation = Quaternion.Euler(0, 0, angle);
}
}
void OnCollisionEnter2D(Collision2D collision)
{
animator.SetTrigger("Death");
dead = true;
deathCooldown = 0.5f;
}
}