我正在我的游戏中实施烟雾预制 . 想法是创建预制件的3个不同实例(在不同位置),然后根据切换按钮销毁它们 . 实例没有被破坏的问题,我不明白为什么 .
这是我的脚本:
class Smoke1 : MonoBehaviour
{
public GameObject myPrefab;
public GameObject canvasObject;
public static GameObject newSmoke1;
public static GameObject newSmoke2;
public static GameObject newSmoke3;
public int toggle1;
public int toggle2;
public int toggle3;
public Vector3 smokeposition1 = new Vector3 (397, -394, 90);
public Vector3 smokeposition2 = new Vector3(414, -402, 90);
public Vector3 smokeposition3 = new Vector3(432, -410, 90);
string newSmoke;
void Start()
{
i = 1;
toggle1 = 2;
toggle2 = 2;
toggle3 = 2;
newinstance(smokeposition1, newSmoke1);
newinstance(smokeposition2, newSmoke2);
newinstance(smokeposition3, newSmoke3);
}
void Update()
{
togglecheck(ThermoElectric.t1Bool, toggle1, newSmoke1, smokeposition1);
togglecheck(ThermoElectric.t2Bool, toggle2, newSmoke2, smokeposition2);
togglecheck(ThermoElectric.t3Bool, toggle3, newSmoke3, smokeposition3);
}
void newinstance(Vector3 smokeposition, GameObject smokeinstance)
{
smokeinstance = Instantiate(myPrefab, smokeposition, Quaternion.Euler(-90, 0, 0)) as GameObject;
smokeinstance.transform.SetParent(canvasObject.transform, false);
smokeinstance.transform.localScale = new Vector3(1, 1, 1);
smokeinstance.GetComponent<ParticleSystem>().startColor = new Color(0.1f, 0.1f, 0.1f, 1);
}
void togglecheck(bool turbine, int togglei, GameObject smokeinstancet, Vector3 smokeposition)
{
if (turbine == true && togglei == 1)
{
newinstance(smokeposition, smokeinstancet);
togglei = 2;
}
if (turbine == false && togglei == 2)
{
Destroy(smokeinstancet, 0.1f);
togglei = 1;
}
}
}
bools正在工作,如果条件得到满足,但预制件不会被破坏 . 你可以帮忙吗?
1 回答
1 . 你不应该摧毁预制件 . 您应该只销毁使用
Instantiate
创建的GameObject .2 . 从GameObject中删除静态 . 我相信你不需要那个 .
如果您是%100,请确保满足if条件,那么
newinstance
函数就是问题 . 如果您不确定,请将Debug.Log
置于if (turbine == false && togglei == 2)
内以验证是否满足此条件,因为没有它,Destroy(smokeinstancet, 0.1f);
将不会被调用 .newinstance
功能有问题 .smokeinstance = Instantiate(myPrefab, smokeposition, Quaternion.Euler(-90, 0, 0)) as GameObject;
代码将实例化的GameObject分配给名为smokeinstance
的参数中的局部变量 . 它不会将它分配给传递给它的GameObjects,例如newSmoke1
,newSmoke2
和newSmoke3
.SOLUTION :
你有2个解决方案:
1 . 使用
ref
或out
关键字 . 在这种情况下,我们将只使用ref
关键字 .然后在调用它时也使用
ref
.2 . 使
newinstance
函数返回GameObject
而不是void
.然后叫它:
任何一个都应该解决你的问题 .