摧毁克隆将摧毁所有克隆
本文关键字:摧毁 | 更新日期: 2023-09-27 17:50:19
我想销毁一个对象的实例,当它在一个特定的圆形区域内。代码如下:
Collider2D[] overlap = Physics2D.OverlapCircleAll(
ball.transform.position,
(ball.renderer.bounds.size.x)/2);
if (overlap.Length>=1)
{
foreach (Collider2D coll in overlap)
{
Debug.Log (coll.GetInstanceID());
if (coll.name.Contains("alien"))
{
//problem here:
Destroy (coll.gameObject);
}
}
}
Destroy(coll.gameObject)
永久地破坏了所有克隆并且没有实例化新的并且我得到错误MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
有没有一种方法可以摧毁那个和那个克隆体?我试过不同的名字和使用Destroy(GameObject.Find(coll.name))
,但这破坏了所有的克隆以及防止新的产卵。
有人帮助吗?
更新:
实例化如下:
private bool bCanCreateParachuter = true; // bool to stop the spawning
GameObject go;
// Use this for initialization
void Start () {
//handling screen orientation
Screen.orientation = ScreenOrientation.LandscapeLeft;
///
go = (GameObject)Instantiate(Resources.Load("alienPink"));
StartCoroutine("CreateParachuter");
}
IEnumerator CreateParachuter()
{
while(bCanCreateParachuter)
{
Instantiate(go, new Vector3(Random.Range(-10,10), Random.Range(-5,5), 0), Quaternion.identity);
// Instantiate(go, new Vector3(Random.Range(-10,10), Random.Range(-10,10), 0), Quaternion.identity);
go.name = "alienPink"+nextNameNumber;
nextNameNumber++;
yield return new WaitForSeconds(Random.Range(0f,1f));
yield return null;
}
yield return null;
}
重要更新:
如果取消
中的if (grabbedObject !=null)
注释,代码将正常工作// if (grabbedObject != null) {
//works if uncomment above for some reason
Collider2D[] overlap = Physics2D.OverlapCircleAll (ball.transform.position, (ball.renderer.bounds.size.x)/2);
if (overlap.Length>=1){
foreach (Collider2D coll in overlap){
Debug.Log (coll.GetInstanceID());
if (coll.name.Contains("alien")){
Destroy (coll.gameObject);
}
}
}else {
// Debug.Log (grabbedObject.renderer.bounds.size.x);
}
这是grabbedObject的背景:
Rigidbody2D grabbedObject = null;
. . .
RaycastHit2D hit = Physics2D.Raycast(mousePos2D , dir);
//if (hit!=null && hit.collider!=null){
// check collisions with aliens
// OnCollisionEnter2D(grabbedObject.collisionDetectionMode);
if ( hit.collider!=null){
// we clicked on something lol... something that has a collider (box2d collider in this case)
if (hit.collider.rigidbody2D!=null){
//hit.collider.rigidbody2D.gravityScale = 1;
grabbedObject = hit.collider.rigidbody2D;
// circleCollider = hit.collider.collider2D. ;
springJoint = grabbedObject.gameObject.AddComponent<SpringJoint2D>();
// set the anchor to the spot on the object that we clicked
Vector3 localHitPoint = grabbedObject.transform.InverseTransformPoint(hit.point);
springJoint.anchor = localHitPoint;
//
dragLine.enabled = true;
}
}
基本上grabbedObject是你点击并拖拽屏幕上的任何东西(任何GameObject),我在这里遗漏了什么?
重生的问题是你没有保存对资源项的引用,所以当你销毁你创建的"模板"要实例化的第一个项时,它就被销毁了
这将解决这个
GameObject template;
void Start()
{
//handling screen orientation
Screen.orientation = ScreenOrientation.LandscapeLeft;
template = (GameObject)Resources.Load("alienPink");
StartCoroutine("CreateParachuter");
}
IEnumerator CreateParachuter()
{
while(bCanCreateParachuter)
{
GameObject go = Instantiate(template, new Vector3(Random.Range(-10,10), Random.Range(-5,5), 0), Quaternion.identity);
go.name = "alienPink"+nextNameNumber;
nextNameNumber++;
yield return new WaitForSeconds(Random.Range(0f,1f));
yield return null;
}
yield return null;
}
就破坏所有克隆而言,你的调试日志是否表明它正在破坏多个项目?如果是这样的话,碰撞可能确实击中了所有的克隆体,从而摧毁了它们。