【问题标题】:Unity method doesn't work when other script accesses?其他脚本访问时Unity方法不起作用?
【发布时间】:2016-05-27 19:22:39
【问题描述】:

我在unity中做了一个多人游戏,并且有一个c#脚本。它包含一个方法public void UpdatePlane 来更新对手的飞机。
在此方法中,我想更改一个值(private bool oppdeadtrue 或者如果布尔值由于任何原因无法工作 int opponentisdeadto 1)或者我根本无法更改其中的值,甚至只是调用方法makeoppdead() 更改那里的值。但正是这些事情在他们应该改变的时候并没有改变。我知道当在方法UpdatePlane 内部时,死亡为真,我收到了日志("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
        }

【问题讨论】:

  • @sstan 请给我一个更好的,我会立即编辑
  • 请详细解释失败的原因。描述当您“尝试执行另一个方法,或将 bool 或 int 设置为另一个值”时发生的行为, 此外,Unity 确实允许您使用带有脚本的调试器。下个断点,单步执行函数,看看逻辑哪里出错了。
  • @ScottChamberlain android studio 'attach to proces' 可以识别我的 wifi 连接设备,但 monoDevelop 不能。只有“统一编辑器”可供选择。根据docs.unity3d.com/Manual/…
  • 好悲痛为什么每个人都反对这个?
  • 我的第一直觉就是“死亡”由于某种原因不是真的……

标签: c# unity3d


【解决方案1】:

感谢您的支持,我可以自己解决它,但方法有点不同:如果我用 (script1).instanceMP.opponentisdead = 1; 更改脚本 2 中脚本 1 的任何值,它会很好地工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-25
    • 1970-01-01
    • 2015-11-03
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多