Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by jocoUnity · Jan 12, 2012 at 11:27 PM · rigidbodyprefabpositiondestroy

Clone of rigid body prefab destruction if at certain position

Not getting my mind wrapped around this despite reading the docs a few times (maybe not reading the right one?).

I have a rigidbody prefab that I clone many times on mouse down.

If any of the clones is at a certain position I want to destroy that clone only.

Here is what I have so far but the clone that satisfies the condition does not get destroyed and in fact I have a null reference on line where I'm checking the position.

 using UnityEngine;
 using System.Collections;
  
 public class MainLoop : MonoBehaviour {
 public Rigidbody projectile;
 public GUITexture newCloneButton;
 Rigidbody clone;
 
     void Update() {
         
         if (Input.GetKeyDown("up")) {
             clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
             clone.position =  new Vector3(0.0F, 10.0f, 3.0f);
         }
         if(clone.rigidbody.transform.position.y < -1.0f ){
             Destroy(clone);
         }
     }
 }


Any help is appreciated.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Berenger · Jan 12, 2012 at 11:38 PM

Try to declare your clone as a GameObject instead, then use clone.transform.

An other solution if you want your clone to be destroyed if it's too low, you can put a box collider at (0, -10, 0), expand it's width and length, then add a script which destroy everything in OnColliderEnter(). If you want the same on each side, you need the box to contain your whole scene, then override OnColliderExit instead.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jocoUnity · Jan 17, 2012 at 11:48 PM 0
Share

Box collider idea was exactly what worked. Good call!

avatar image
1

Answer by robbiek · Jan 13, 2012 at 01:22 AM

Have you simply tried Destroy(gameobject); instead of clone?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jocoUnity · Jan 17, 2012 at 11:50 PM 0
Share

I haven't tried but I will just to see if there is more than one way to do this. Thanks.

avatar image
0

Answer by hunter789 · Jan 18, 2012 at 02:17 AM

I'm new to unity (not to programming/game programming) but I've noticed this:

  • You create many clones (one per frame is the mouse is down), but you keep track of only one.
  • You check only if the last clone has to be deleted, whether it is this same frame or a previous one.
  • If the last clone created fulfills the condition and is destroyed, and no more clone is created just the frame after, your clone is a destroyed object.

I haven't tested it in Unity, but wrote it in visual studio, so there should be no syntax errors:

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class MainLoop : MonoBehaviour { public Rigidbody projectile; public GUITexture newCloneButton; List<Rigidbody> clones; // keep track of ALL your clones

 void Update()
 {
     List&lt;Rigidbody&gt; removeItems; // needed for clean up

     if (Input.GetKeyDown("up"))
     {
         Rigidbody clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
         clone.position = new Vector3(0.0F, 10.0f, 3.0f);

         clones.Add(clone);
     }

     // http://stackoverflow.com/a/4868062/637987
     removeItems = new List&lt;Rigidbody&gt;();
     foreach (Rigidbody clone in clones)
     {
         if (clone.rigidbody.transform.position.y &lt; -1.0f)
         {
             removeItems.Add(clone); // make a list of all the clones you have to destroy
         }
     }

     // Destroy all the clones that have been flagged
     foreach (Rigidbody clone in removeItems)
     {
         Destroy(clone);
         clones.Remove(clone);
     }

 }

}

You can't remove the clones directly in the main list because if you remove an element while you look through the list, you change it's composition and behavior gets messed up (in short, it blows up :P). This is why you first have to make a new list, then with the new list modify the first one.

I hope it helps :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

problem whit Instantiate Prefabs position. 0 Answers

Removing A Component From An Instantiated Prefab After X More Are Instantiated 1 Answer

Checking Position 1 Answer

Destroy an instance of a prefab 1 Answer

How can I translate my prefab on Instantiate? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges