Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# bool即使在被调用更改时也保持为false_C#_Unity3d - Fatal编程技术网

C# bool即使在被调用更改时也保持为false

C# bool即使在被调用更改时也保持为false,c#,unity3d,C#,Unity3d,更新 这不是我的主要问题。退房 我在unity中制作了一个多人游戏,我想知道对手玩家何时死亡(bool oppdead) 如果我运行我的代码,对手玩家死了,我会得到日志“opp player is dead”,但我的onGUI没有被执行。我做错什么了吗?我的其他孩子都做得很好 using UnityEngine; using System.Collections; using System.Collections.Generic; using GooglePlayGames.BasicApi.M

更新 这不是我的主要问题。退房

我在unity中制作了一个多人游戏,我想知道对手玩家何时死亡(bool oppdead)

如果我运行我的代码,对手玩家死了,我会得到日志“opp player is dead”,但我的onGUI没有被执行。我做错什么了吗?我的其他孩子都做得很好

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;
    }


}
使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
使用GooglePlayGames.BasicApi.Multiplayer;
使用UnityEngine.UI;
使用制度;
公共类鸟类移动MP:单行为,MPLobbyListener
{
公共游戏对象对立面;
公共刚体二维平面;
私人多层公寓;
私有字符串_myParticipantId;
公共bool-oppdead=false;
公共浮子薄片=100f;
公共浮球前进速度=1f;
bool-didFlap=false;
动画师;
公共布尔死=假;
浮子死亡冷却;
公共刚体二维平面;
公共按钮尺寸;
公开播放;
公众健康之家;
void SetupMultiplayerGame()
{
MultiplayerController.Instance.lobbyListener=此;
// 1
_myParticipantId=MultiplayerController.Instance.GetMyParticipantId();
// 2
List allPlayers=MultiplayerController.Instance.GetAllPlayers();
_opponentScripts=新字典(allPlayers.Count-1);
for(int i=0;i0)
{
transform.rotation=Quaternion.Euler(0,0,0);
}
其他的
{
浮动角=数学勒普(0,-90,(-平面速度y/3f));
transform.rotation=Quaternion.Euler(0,0,角度);
}
}
空心OnCollisionInter2D(碰撞2D碰撞)
{
动画师。设置触发器(“死亡”);
死=真;
死亡冷却时间=0.5f;
}
}
(一个)您面临的问题很简单,不要为“Inspector变量”设置默认值(即,当您有“public”时)

如果(见下文)您需要一个Inspector变量
public int example = 3;
public int example;
public bool oppdead = false;
[System.NonSerialized] public bool oppdead = false;
[System.NonSerialized] public
public
 private bool oppdead
public void devMessage(string m)
{
Text t = GameObject.Find("devText").GetComponent<Text>();
t.text = t.text +"\n" +m
}