我正在游戏中实施烟雾预制。想法是创建预制件的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;
}
}
}
bool正在工作,如果条件得到满足,但预制件不会被破坏。你可以帮忙吗?
答案 0 :(得分:1)
bool正在工作,if条件得到满足,但预制件 不要被破坏。你可以帮忙吗?
1 。你不应该摧毁预制件。您应该只销毁使用Instantiate
创建的GameObject。
2 。从GameObject中删除静态。我相信你不需要那个。
如果您是%100,请确保满足if条件,那么问题就是newinstance
。如果您不确定,请将Debug.Log
放在if (turbine == false && togglei == 2)
内以验证这一点条件得到满足,因为没有它,Destroy(smokeinstancet, 0.1f);
不会被调用。
newinstance
功能问题。
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);
}
smokeinstance = Instantiate(myPrefab, smokeposition, Quaternion.Euler(-90, 0, 0)) as GameObject;
代码将实例化的GameObject分配给名为smokeinstance
的参数中的局部变量。它不会将其分配给传递给它的GameObjects,例如newSmoke1
,newSmoke2
和newSmoke3
。
newinstance(smokeposition1, newSmoke1);
newinstance(smokeposition2, newSmoke2);
newinstance(smokeposition3, newSmoke3);
<强>解强>:
您有2个解决方案:
1 。使用ref
或out
关键字。在这种情况下,我们只会使用ref
关键字。
void newinstance(Vector3 smokeposition, ref GameObject smokeinstance)
{
......
}
然后在调用时使用ref
。
newinstance(smokeposition1, ref newSmoke1);
newinstance(smokeposition2, ref newSmoke2);
newinstance(smokeposition3, ref newSmoke3);
2 。使newinstance
函数返回GameObject
而不是void
。
GameObject newinstance(Vector3 smokeposition)
{
GameObject smokeinstance = Instantiate(myPrefab, smokeposition, Quaternion.Euler(-90, 0, 0));
}
然后调用它:
newSmoke1 = newinstance(smokeposition1);
newSmoke2 = newinstance(smokeposition2);
newSmoke3 = newinstance(smokeposition3);
任何一个都可以解决你的问题。