其他脚本访问时,Unity方法不起作用? [英] Unity method doesn't work when other script accesses?
问题描述
我已经统一制作了一个多人游戏,并且有一个C#脚本.它包含用于更新对手平面的方法public void UpdatePlane
.
在此方法内部,我想更改一个值(将private bool oppdead
更改为true
,或者如果只是布尔变量出于任何原因将int opponentisdead
更改为1
),或者如果我根本无法更改其中的值,甚至只需调用method makeoppdead()
在那里更改值.但正是这些事情在应该改变的时候并没有改变.我知道当在方法UpdatePlane
中时,死亡变为真,我收到日志("oppplayer dead is true")
,但值没有改变,并且方法makeoppdead()
没有执行,我没有得到log ("method was called")
.另一方面,对我来说令人惊讶的是,它可以毫无问题地转换opponentPrefab
.这是代码:
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 true
or if just bools do not work for any reason int opponentisdead
to 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
}
推荐答案
感谢您的支持,我能够以一种不同的方式自行解决此问题:如果我更改任何值,它都可以很好地工作使用(script1).instanceMP.opponentisdead = 1;
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;
这篇关于其他脚本访问时,Unity方法不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!