其他脚本访问时Unity方法不起作用?

时间:2016-05-27 19:22:39

标签: c# unity3d

我已经制作了一个统一的多人游戏,并且有一个c#脚本。它包含一个方法public void UpdatePlane来更新对手的飞机 在此方法中,我想要将值private bool oppdead更改为true,或者只是bools因任何原因int opponentisdead1不起作用,或者我无法更改值在那里甚至只需调用方法makeoppdead()来改变那里的值。但确实那些东西在他们应该的时候不会改变。我知道在方法UpdatePlane内部,死亡变为真,我收到了Log ("oppplayer dead is true")但是值没有变化,方法makeoppdead()没有被执行,我没有得到记录("method was called")。另一方面,令我惊讶的是,它可以毫无问题地转换opponentPrefab。这是代码:

        public GameObject opponentPrefab;
        public Rigidbody2D oppPlane;

        private bool _multiplayerReady;
        private string _myParticipantId;


        private bool oppdead;
       int opponentisdead = 0;
    bool boolopponentisdead;

        float opponentdistance;
        float distancex;
        float distancey;
        float distance;

        public void UpdatePlane(string senderId, float x, float y, float z, bool death, float oppdistance)
        {

            MultiplayerController.Instance.GetMyParticipantId();

            opponentdistance = oppdistance;
                if (death)
            {

           //this stuff is NOT being executed:

                makeoppdead();
            opponentisdead = 1;
    boolopponentisdead = true;

    //but I do receive this message:
            Debug.Log("oppplayer dead is true");


            }
           //this stuff is being executed:

                opponentPrefab = GameObject.Find("Opponent");

                opponentPrefab.transform.position = new Vector3(x, y, 0);
                    opponentPrefab.transform.rotation = Quaternion.Euler(0, 0, z);




        }
        void makeoppdead()
        {
Debug.Log("method was called");
    //do some stuff to provide he is really dead
        }

1 个答案:

答案 0 :(得分:0)

感谢您的支持,我能够以一种不同的方式解决这个问题:如果我使用(script1).instanceMP.opponentisdead = 1;更改脚本2中脚本1的任何值,它可以很好地解决它p>