Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 游戏对象在没有碰撞的情况下激活。如何在场景开始时禁用游戏对象激活?_C#_Unity3d_Game Development - Fatal编程技术网

C# 游戏对象在没有碰撞的情况下激活。如何在场景开始时禁用游戏对象激活?

C# 游戏对象在没有碰撞的情况下激活。如何在场景开始时禁用游戏对象激活?,c#,unity3d,game-development,C#,Unity3d,Game Development,我试图激活游戏对象的基础上,相机作为对撞机和对撞机的触发。这是我为此编写的简单脚本。它工作得很好,但是默认情况下会激活游戏对象,我必须通过一次进入/退出来停用它们 using System.Collections; using System.Collections.Generic; using UnityEngine; public class reveal : MonoBehaviour { public GameObject Stand; /// <summary> ///

我试图激活游戏对象的基础上,相机作为对撞机和对撞机的触发。这是我为此编写的简单脚本。它工作得很好,但是默认情况下会激活游戏对象,我必须通过一次进入/退出来停用它们

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class reveal : MonoBehaviour
{

public GameObject Stand;

/// <summary>
/// OnTriggerEnter is called when the Collider other enters the trigger.
/// </summary>
/// <param name="other">The other Collider involved in this collision.</param>
void OnTriggerEnter(Collider other)
{
    Stand.SetActive(true);
}

void OnTriggerExit(Collider other)
{
    Stand.SetActive(false);
}


}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公开课:单一行为
{
公众游戏者看台;
/// 
///当碰撞器进入触发器时,调用OnTriggerCenter。
/// 
///与这次碰撞有关的另一台对撞机。
无效对撞机(对撞机其他)
{
Stand.SetActive(真);
}
void OnTriggerExit(碰撞器其他)
{
Stand.SetActive(假);
}
}
为了解决这个问题,我修改了脚本,在场景开始之前停用游戏对象,这里的问题是触发器/碰撞器交互不再工作。我想我添加的代码只是永久地停用了对象

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class reveal : MonoBehaviour
{
public GameObject Stand;
// Start is called before the first frame update
void Start()
{
    Stand = GameObject.Find("Stand");
    Stand.gameObject.SetActive(false);

}

//public GameObject Confrontation;

/// <summary>
/// OnTriggerEnter is called when the Collider other enters the trigger.
/// </summary>
/// <param name="other">The other Collider involved in this collision.</param>
void OnTriggerEnter(Collider other)
{
    Stand.SetActive(true);
}
void OnTriggerExit(Collider other)
{
    Stand.SetActive(false);
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公开课:单一行为
{
公众游戏者看台;
//在第一帧更新之前调用Start
void Start()
{
Stand=游戏对象。查找(“Stand”);
Stand.gameObject.SetActive(false);
}
//公众游戏对象对抗;
/// 
///当碰撞器进入触发器时,调用OnTriggerCenter。
/// 
///与这次碰撞有关的另一台对撞机。
无效对撞机(对撞机其他)
{
Stand.SetActive(真);
}
void OnTriggerExit(碰撞器其他)
{
Stand.SetActive(假);
}
}

实现这一点的正确方法是什么??有什么想法吗

为了能够继续使用脚本,您可以创建另一个空游戏对象,并将脚本添加到该对象中,而不是直接添加到要禁用的对象上

我不确定我是否完全理解你的问题,如果你只是想隐藏游戏对象,这样你就看不到它,但仍然能够与对撞机进行交互。你可以这样做

 void OnTriggerEnter(Collider other)
    {
        Stand.GetComponent<MeshRenderer>().enabled = true;
    }
    void OnTriggerExit(Collider other)
    {
        Stand.GetComponent<MeshRenderer>().enabled = false;   
}

 }
void ontriggenter(碰撞器其他)
{
Stand.GetComponent().enabled=true;
}
void OnTriggerExit(碰撞器其他)
{
Stand.GetComponent().enabled=false;
}
}

请注意,如果您将脚本附加到游戏对象,如果您尝试调用
SetActive(),它将不会再次激活我已将脚本附加到碰撞器与之交互的单独触发器对象上。感谢您的响应和该脚本,CubeCrafter360。我有一个脚本附加的触发器对象,碰撞器与之交互并控制对象支架的激活(如果有意义)。相机/播放器有对撞机。希望这是清楚的。你所建议的是SetActive(真/假)的替代方案是我的猜测。我的主要问题是,当场景开始时,没有任何交互,游戏对象已经被激活,即使我在默认情况下关闭了它们。