Tôi muốn phá hủy một thể hiện của một đối tượng khi nó nằm trong một vùng tròn cụ thể. Mã này là như sau:Phá hủy bản sao phá hủy tất cả các dòng vô tính
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);
}
}
}
các Destroy(coll.gameObject)
phá hủy tất cả các dòng vô tính permanantly và những người mới không được khởi tạo và tôi nhận được lỗi 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.
Có cách nào để tiêu diệt đó và bản sao trong cụ thể? Tôi đã thử các tên khác nhau và sử dụng Destroy(GameObject.Find(coll.name))
nhưng phá hủy tất cả các bản sao cũng như ngăn chặn những người mới sinh sản.
Ai đó giúp đỡ?
UPDATE:
Instantiating như sau:
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;
}
Cập nhật Crucial:
Mã này hoạt động nếu tôi bỏ ghi chú if (grabbedObject !=null)
trong
// 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);
}
Đây là nền tảng của 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;
}
}
Về cơ bản các grabbedObject là bất cứ điều gì bạn nhấp và kéo dài trên màn hình (bất kỳ GameObject), những gì tôi thiếu ở đây chàng trai?
Bạn tạo bản sao của 'gameObject' như thế nào? – Dai
Có thể có sự cố với mã "nhân bản" của bạn ... hoặc 'Phá hủy' - đảm bảo đăng tập hợp mã nhỏ nhất có thể hiển thị sự cố với tất cả các phần có liên quan. –
Cập nhật bài viết gốc để bao gồm instantiation –