C# 如何将游戏对象设置为非活动状态,然后在unity中几秒钟后激活?

C# 如何将游戏对象设置为非活动状态,然后在unity中几秒钟后激活?,c#,unity3d,C#,Unity3d,当玩家与游戏对象发生碰撞时,我想使游戏对象处于非活动状态(我已经这样做了)。现在我想等待几秒钟,然后再次激活它。我该怎么做 这是我的密码 using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement: MonoBehaviour { private float chubbyScore = 0; private float coin = 0;

当玩家与游戏对象发生碰撞时,我想使游戏对象处于非活动状态(我已经这样做了)。现在我想等待几秒钟,然后再次激活它。我该怎么做

这是我的密码

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

public class PlayerMovement: MonoBehaviour
{

private float chubbyScore = 0;
private float coin = 0;
public float speed = 1;
public float jump = 1;

void Update()
{
    Vector3 playerMovement = Vector3.zero;
    playerMovement.x = Input.GetAxis("Horizontal");
    transform.position += playerMovement * speed * Time.deltaTime;
    transform.Translate(Vector3.forward * Time.deltaTime * speed);

    if (Input.GetKeyDown(KeyCode.Space))
    {
        GetComponent<Rigidbody>().velocity = Vector3.up * jump;
    }
}

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag("Pick Up"))
    {
        other.gameObject.SetActive(false);
        coin += 1;
        chubbyScore += 50;
    } 

}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类玩家运动:单一行为
{
私人浮球圆心=0;
私人浮币=0;
公共浮动速度=1;
公共浮动跳跃=1;
无效更新()
{
Vector3 playerMovement=Vector3.0;
playerMovement.x=输入.GetAxis(“水平”);
transform.position+=播放器移动*速度*时间.deltaTime;
transform.Translate(Vector3.forward*Time.deltaTime*speed);
if(Input.GetKeyDown(KeyCode.Space))
{
GetComponent().velocity=Vector3.up*跳跃;
}
}
无效对撞机(对撞机其他)
{
if(other.gameObject.CompareTag(“拾取”))
{
other.gameObject.SetActive(false);
硬币+=1;
圆心+=50;
} 
}
}

我将为偏移持续时间创建一个公共浮点,然后您可以为计时器创建一个私有浮点

设置对象活动状态的代码可能如下所示:

private float chubbyScore = 0;
private float coin = 0;
public float speed = 1;
public float jump = 1;
public float offsetTime = 2f;
private float timer = 0f;
private GameObject collObj;

void Update()
{
    Vector3 playerMovement = Vector3.zero;
    playerMovement.x = Input.GetAxis("Horizontal");
    transform.position += playerMovement * speed * Time.deltaTime;
    transform.Translate(Vector3.forward * Time.deltaTime * speed);

    if (Input.GetKeyDown(KeyCode.Space))
    {
        GetComponent<Rigidbody>().velocity = Vector3.up * jump;
    }

    if(!collObj.active)
    {
        timer += Time.deltaTime;
        if(timer > offsetTime)
        {
            timer = 0f;
            collObj.SetActive(true);
        }
    }
}

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag("Pick Up"))
    {
        collObj = other.gameObject;
        collObj.SetActive(false);
        coin += 1;
        chubbyScore += 50;
    }

}
private float chubbyScore=0;
私人浮币=0;
公共浮动速度=1;
公共浮动跳跃=1;
公共浮动偏移时间=2f;
专用浮点计时器=0f;
私有游戏对象collObj;
无效更新()
{
Vector3 playerMovement=Vector3.0;
playerMovement.x=输入.GetAxis(“水平”);
transform.position+=播放器移动*速度*时间.deltaTime;
transform.Translate(Vector3.forward*Time.deltaTime*speed);
if(Input.GetKeyDown(KeyCode.Space))
{
GetComponent().velocity=Vector3.up*跳跃;
}
如果(!collObj.active)
{
timer+=Time.deltaTime;
如果(计时器>偏移时间)
{
定时器=0f;
collObj.SetActive(真);
}
}
}
无效对撞机(对撞机其他)
{
if(other.gameObject.CompareTag(“拾取”))
{
collObj=other.gameObject;
collObj.SetActive(假);
硬币+=1;
圆心+=50;
}
}

创建一个简单的等待通用实用程序,如下所示:

public class Waiter : MonoBehaviour
{
    static Waiter instance = null;
    static Waiter Instance
    {
        get
        {
            if (instance == null)
                instance = new GameObject("Waiter").AddComponent<Waiter>();
            return instance;
        }
    }
    private void Awake()
    {
        instance = this;
    }
    private void OnDestroy()
    {
        if (instance == this)
            instance = null;
    }

    IEnumerator WaitRoutine(float duration, System.Action callback)
    {
        yield return new WaitForSeconds(duration);
        callback?.Invoke();
    }

    public static void Wait(float seconds, System.Action callback)
    {
        Instance.StartCoroutine(Instance.WaitRoutine(seconds, callback));
    }
}

创建一个新功能,在其中启用/禁用游戏对象。然后在start方法中重复调用最近生成的函数

//Enables or Disables the GameObject every 5 seconds
void Start()
{
     InvokeRepeating("Repeat", 0, 5);
}

void Repeat()
{
     //Enable or Disable the GameObject
}

我试过了,但没有成功<代码>IEnumerator ActivateBlock(){yield return new WaitForSeconds(0.5f);if(gameObject.CompareTag(“Pick”){gameObject.SetActive(true);}}}}
//Enables or Disables the GameObject every 5 seconds
void Start()
{
     InvokeRepeating("Repeat", 0, 5);
}

void Repeat()
{
     //Enable or Disable the GameObject
}