Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

c# - Unity method doesn't work when other script accesses?

I have made a multiplayer game in unity, and have a c# script. It contains a method public void UpdatePlane to update the opponents plane.
Inside this method, I want to change a value (private bool oppdead to trueor if just bools do not work for any reason int opponentisdeadto 1) or if I cant change values in there at all even just call the methodmakeoppdead() to change the values there. but exactly those things do not change when they should. I know when inside the method UpdatePlane, death becomes true, I receive the Log ("oppplayer dead is true")but the values doesnt change and the methodmakeoppdead() is not beeing executed, I'm not getting the log("method was called") . Surprisingly for me, on the other hand, it can transform the opponentPrefab without any problems. here's the code:

        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
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thanks for your support, I was able to solve it at my own, with a little different way: it works very well, if I change the value of anything of script 1 in script 2 with (script1).instanceMP.opponentisdead = 1;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...