【发布时间】:2016-06-01 18:23:07
【问题描述】:
我需要使用脚本中定义的一些变量,但是当我调用它们时,它们的值为 0。我不知道我做错了什么。
例子:
Script1.cs
public int cont;
public void Method() { cont++; }
void Update() { Method(); }
Script2.cs
public Script1 usingScript1;
void MethodX()
{
usingScript1.GetComponent<Script1>();
Debug.Log(usingScript1.cont);
}
void Update() { MethodX(); }
这个脚本应该显示“cont”变量增加,因为它是从 Update() 调用的,但是这没有发生。当我调用它时,它是0并且不增加。
另外,我在 Ispector 中引用了包含 Script1.cs 的对象。这一定是我想念的一件简单的事情。我什至尝试调用 Method()。
【问题讨论】:
-
我投票决定将此问题作为题外话结束,因为它重复了无数次。有趣的是,这是尝试 Unity 的新爱好者提出的关于 Unity 的最常见问题。
-
“永远不要使用更新...专家永远不需要使用它” - 不正确。 "Update is the most commonly used function to implement any kind of game behaviour"
-
嗨@JoeBlow,你是说在涉及
RigidBodies时避免Update吗?在那种情况下,我同意你的看法。否则,覆盖Update并没有什么问题,正如您在我的漂亮 GPGPU n-Body galaxy simulation 中看到的那样。 :)
标签: c# variables unity3d scripting