在使用SetActive()函数时,发现了一个有意思的小bug。
我把代码挂在物体上,想实现这个物体忽闪忽现的效果。
然而。
当使用SetActive(false)之后,就没办法使用SetActive(true)了。
也就是说,一旦这个物体被禁止后,挂在这个物体身上的脚本也不能运行了,不过,如果把脚本挂在其他物体上执行SetActive(false)和SetActive(true)就没问题了。
代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public GameObject cub;
int n=0;
void Start()
{
}
void Update()
{
n++;
if (n % 30==0)
{
cub.gameObject.SetActive(false);
}
if (n % 59 == 0)
{
cub.gameObject.SetActive(true);
n = 0;
}
}
}