将gameObject设置为活动Unity C#
本文关键字:Unity 活动 gameObject 设置 | 更新日期: 2023-09-27 18:27:31
游戏对象受到正方向的力,一段时间后受到负方向的力。
如果力是在负方向上施加的,这意味着游戏结束,我想显示一个完全不同的gameObject,也就是所谓的icetextureONfile。我的方法不起作用,我得到错误"类型'UnityEngine.GameObject'不包含icetextureONfile的定义"。我很难找到
public void FixedUpdate() {
// No action happened yet
gameObject.icetextureONfile.SetActive (false);
// Increase the kick timer
kickTimer += Time.fixedDeltaTime;
// If the next kick time has came
if (nextKick < kickTimer) {
// Schedule the kick back corresponding to the current kick
nextKickBacks.Enqueue (nextKick + 100f);
// Apply the kick force
rb.AddForce (transform.up * thrust, ForceMode.Impulse);
// Plan the next kick
nextKick = kickTimer + Random.Range (MinKickTime, MaxKickTime);
}
// If there are more kick backs to go, and the time of the closest one has came
if (0 < nextKickBacks.Count) {
if (nextKickBacks.Peek () < kickTimer) {
// Apply the kick back force
rb.AddForce (-transform.up * thrust, ForceMode.Impulse);
// Game object was kicked down, set active game over object
gameObject.icetextureONfile.SetActive (true);
// Dequeue the used kick back time
nextKickBacks.Dequeue ();
}
}
}
如果你想停用一个,然后激活另一个,你可以把它添加到类中,确保它不在函数中
public GameObject iceTexture;
然后将该对象拖放到脚本中显示的名为iceTexture的位置。然后,只需确保停用脚本所附加的对象并激活iceTexture对象。
gameObject.SetActive(false);
iceTexture.SetActive(true);
此页面可能会对您有所帮助。
此语法适用于我的
GameObject.Find ("icetextureONfile").SetActive(false);
与Unity文档中的内容相反
gameObject.SetActive(false);
第一个对我有效,第二个没有。我不是一个伟大的程序员,所以也许这只是一个上下文问题。
编辑众所周知,在对象已经设置为活动(false)之后,很难设置活动(true)。这是因为附加的脚本也被停用了。有关为什么这是一场噩梦的详细信息,请参阅Unity论坛。
为了克服这一点,我选择了一条不同的路线来完成同样的事情。
我将对象的大小设置为0,直到我需要它为止
GameObject.Find ("icetextureONfile").transform.localScale = new Vector3(0, 0, 0);
GameObject.Find ("icetextureONfile").transform.localScale = new Vector3(0.02f, 0.02f, 0.02f);
请注意,0.02是我的对象的大小,可能与您的对象大小不完全相同。