从脚本 Unity3d C# 中销毁脚本创建的对象
Destroying an object created by a script from within the script Unity3d C#
破败
好的,伙计们需要一点帮助。基本上,我是在一个开放位置的预制件中实例化一个 healthPack。经过一段时间后,如果没有拾取 HealthPack,我会尝试销毁它,将 bool healthPackExist 设置回 false,并在空闲位置实例化另一个 healthPack。
问题:
当我尝试访问正在实例化的游戏对象时,我最终要么破坏了整个父层次结构,要么只是擦掉了脚本。
解决方案
我试过销毁根对象,搜索创建对象的名称,向对象添加标签(Health Pack)并搜索它总是出错。
代码如下:
public GameObject healthPackPrefab;
public GameObject health;
private float healthTimer;
private bool healthExist;
// Use this for initialization
void Start ()
{
//set healthExist to false to indicate no health packs exist on game start
healthExist = false;
}
// Update is called once per frame
void Update ()
{
//first check to see if a health pack exist, if not call method to spawn a health pack
//otherwise check to see if one exist and if it does is it's timer created with it
//has been passed by Time.time (returns game clock time), if yes destroy the created
//health pack.
if (healthExist == false)
{
spawnUntilFull ();
}
else if (healthExist == true && Time.time > healthTimer)
{
//Error occuring here when trying to destroy
//Destroy(transform.root.gameObject) //destroys script, object scripts on, and prefab
Destroy(this.gameObject); //does same as Destroy(transform.root.gameObject
healthExist = false;
}
}
Transform NextFreePosition()
{
//free space
foreach (Transform childPositionGameObject in transform)
{
//if no health packs located return location of child object to spawn new health pack
if (childPositionGameObject.childCount == 0)
{
return childPositionGameObject;
}
}
return null;
}
void spawnUntilFull()
{
//returns next free position in space
Transform freePosition = NextFreePosition ();
//if free slot is available
if (freePosition && healthExist == false)
{
//instantiates health object and places it in scene at coordinates received from
//freePosition
health = Instantiate (healthPackPrefab, freePosition.position, Quaternion.identity) as GameObject;
//spawns enemy onto a position under the Parent (Enemy Formation)
health.transform.parent = freePosition;
//set bool to true to stop spawning
healthExist = true;
//seat HealthTimer to 5 seconds after creation for use in Update method
healthTimer = Time.time + 5.0f;
}
}
当您调用 Destroy() 时,您实际上在做的是销毁脚本。要实现你想要的(销毁医疗包),你只需调用 Destroy 即可:
Destroy(health);
附带说明一下,为了避免使用 Time.time
的混乱代码,Destroy() 有一个使用两个参数的重载:
Destroy(GameObject object, float delay);
这应该有助于简化您的代码并使其更具可读性。
您可以在预制件上添加一个单独的脚本,其中包含您自定义的 DestroyObject 方法。
对象创建后立即在该脚本上启动计时器。
现在,如果在一定时间内没有收集到,您可以轻松销毁该对象。
破败 好的,伙计们需要一点帮助。基本上,我是在一个开放位置的预制件中实例化一个 healthPack。经过一段时间后,如果没有拾取 HealthPack,我会尝试销毁它,将 bool healthPackExist 设置回 false,并在空闲位置实例化另一个 healthPack。
问题: 当我尝试访问正在实例化的游戏对象时,我最终要么破坏了整个父层次结构,要么只是擦掉了脚本。
解决方案 我试过销毁根对象,搜索创建对象的名称,向对象添加标签(Health Pack)并搜索它总是出错。
代码如下:
public GameObject healthPackPrefab;
public GameObject health;
private float healthTimer;
private bool healthExist;
// Use this for initialization
void Start ()
{
//set healthExist to false to indicate no health packs exist on game start
healthExist = false;
}
// Update is called once per frame
void Update ()
{
//first check to see if a health pack exist, if not call method to spawn a health pack
//otherwise check to see if one exist and if it does is it's timer created with it
//has been passed by Time.time (returns game clock time), if yes destroy the created
//health pack.
if (healthExist == false)
{
spawnUntilFull ();
}
else if (healthExist == true && Time.time > healthTimer)
{
//Error occuring here when trying to destroy
//Destroy(transform.root.gameObject) //destroys script, object scripts on, and prefab
Destroy(this.gameObject); //does same as Destroy(transform.root.gameObject
healthExist = false;
}
}
Transform NextFreePosition()
{
//free space
foreach (Transform childPositionGameObject in transform)
{
//if no health packs located return location of child object to spawn new health pack
if (childPositionGameObject.childCount == 0)
{
return childPositionGameObject;
}
}
return null;
}
void spawnUntilFull()
{
//returns next free position in space
Transform freePosition = NextFreePosition ();
//if free slot is available
if (freePosition && healthExist == false)
{
//instantiates health object and places it in scene at coordinates received from
//freePosition
health = Instantiate (healthPackPrefab, freePosition.position, Quaternion.identity) as GameObject;
//spawns enemy onto a position under the Parent (Enemy Formation)
health.transform.parent = freePosition;
//set bool to true to stop spawning
healthExist = true;
//seat HealthTimer to 5 seconds after creation for use in Update method
healthTimer = Time.time + 5.0f;
}
}
当您调用 Destroy() 时,您实际上在做的是销毁脚本。要实现你想要的(销毁医疗包),你只需调用 Destroy 即可:
Destroy(health);
附带说明一下,为了避免使用 Time.time
的混乱代码,Destroy() 有一个使用两个参数的重载:
Destroy(GameObject object, float delay);
这应该有助于简化您的代码并使其更具可读性。
您可以在预制件上添加一个单独的脚本,其中包含您自定义的 DestroyObject 方法。
对象创建后立即在该脚本上启动计时器。
现在,如果在一定时间内没有收集到,您可以轻松销毁该对象。