C# 如何在另一个函数中访问OnTiggerEnter2D中的值? 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 公共类RedHP:单行为 { 公共浮动HP=5; 公共游戏对象蓝赢; 公共游戏对象重启; 无效OnTriggerEnter2D(碰撞R2D触发器) { 如果(trig.gameObject.tag==“ThrowableBlue”) { 开始例行程序(时间()); 惠普--; 如果(HP
),则不必这样做。该函数可以在对象被销毁之前使用第二个参数作为延迟时间C# 如何在另一个函数中访问OnTiggerEnter2D中的值? 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 公共类RedHP:单行为 { 公共浮动HP=5; 公共游戏对象蓝赢; 公共游戏对象重启; 无效OnTriggerEnter2D(碰撞R2D触发器) { 如果(trig.gameObject.tag==“ThrowableBlue”) { 开始例行程序(时间()); 惠普--; 如果(HP,c#,unity3d,destroy,gameobject,ienumerator,C#,Unity3d,Destroy,Gameobject,Ienumerator,),则不必这样做。该函数可以在对象被销毁之前使用第二个参数作为延迟时间 using System.Collections; using System.Collections.Generic; using UnityEngine; public class RedHP : MonoBehaviour { public float HP = 5; public GameObject BlueWon; public GameObject Restart; void
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RedHP : MonoBehaviour
{
public float HP = 5;
public GameObject BlueWon;
public GameObject Restart;
void OnTriggerEnter2D(Collider2D trig)
{
if (trig.gameObject.tag == "ThrowableBlue")
{
StartCoroutine(BowlDestroyTime());
HP--;
if (HP <= 0)
{
BlueWon.SetActive(true);
Restart.SetActive(true);
PlayerBlueController.canMove = false;
PlayerBlueController.canFire = false;
}
}
}
IEnumerator BowlDestroyTime()
{
yield return new WaitForSeconds(1);
Destroy(trig.gameObject);
}
}
Destroy(trig.gameObject, 1f);
如果仍要使用协同程序执行此操作,只需使
BowlDestroyTime
函数以GameObject
作为参数,然后将游戏对象从OnTriggerEnter2D
函数传递到要销毁的BowlDestroyTime
函数
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RedHP : MonoBehaviour
{
public float HP = 5;
public GameObject BlueWon;
public GameObject Restart;
void OnTriggerEnter2D(Collider2D trig)
{
if (trig.gameObject.tag == "ThrowableBlue")
{
StartCoroutine(BowlDestroyTime());
HP--;
if (HP <= 0)
{
BlueWon.SetActive(true);
Restart.SetActive(true);
PlayerBlueController.canMove = false;
PlayerBlueController.canFire = false;
}
}
}
IEnumerator BowlDestroyTime()
{
yield return new WaitForSeconds(1);
Destroy(trig.gameObject);
}
}
Destroy(trig.gameObject, 1f);
void OnTriggerEnter2D(碰撞R2D触发器)
{
如果(trig.gameObject.tag==“ThrowableBlue”)
{
start例程(触发时间(trig.gameObject));
惠普--;
如果(HP