• 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
0
Question by jbickel · Aug 11, 2011 at 12:08 AM · rigidbodycolliderdestroy

Why are collisions not working?

Sure. Some might say this question has been asked too many times to bother with (I read "ad nauseum" in one post), but the documentation and all google searches yield no answers.

Anyways, I used the template from the documentation in order to create a rigidbody projectile...

 // Instantiate a rigidbody then set the velocity
 
 var projectile : Rigidbody;
 
 function Update () {
 // Ctrl was pressed, launch a projectile
 if (Input.GetButtonDown("Fire1")) {
 // Instantiate the projectile at the position and rotation of this transform
 var clone : Rigidbody;
 clone = Instantiate(projectile, transform.position, transform.rotation);
 
 // Give the cloned object an initial velocity along the current
 // object's Z axis
 clone.velocity = transform.TransformDirection (Vector3.forward * 10);
 }
 }

And this one for the collision which is attached to my terrain...

 function OnCollisionEnter (other : Collider) {
     
     if (other.name == "clone") {
         Destroy (other.gameObject);
         }
     }

From what I have gleaned from both the documentation, google searches, and unity answers searches, this would seem the way to do it. But apparently it's not. My gut tells me it has to something to do with the fact that I am make a new rigidbody from a prefab (clone), and that because of that, I need to do something else to access each individual "clone". I don't know as I have been starting at this issue for too long for it to make any sense... As of now, my projectiles bounce off of my terrain.

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 aldonaletto · Aug 11, 2011 at 12:32 AM

You're comparing apples and oranges. clone is the variable's name, not the name of the object you've instantiated (its name is the same of the prefab, perhaps with a "(clone)" string appended - Unity has misterious reasons to append it or not).
A simple way to work around this is to name explicitly the object right after its creation:

  var clone : Rigidbody;
  clone = Instantiate(projectile, transform.position, transform.rotation);
  clone.name = "clone";
The last line defines the object name to "clone", thus your comparison can work.
In Unit, the object name is a String variable (property, to be more specific) which can be accessed with collider.name or transform.name (read inherited variables in Collider or Transform in the docs)
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 Eric5h5 · Aug 11, 2011 at 01:42 AM 0
Share

When you instantiate a prefab, it always has "(Clone)" appended, unless you rename it yourself.

avatar image
0

Answer by Blankzz · Aug 11, 2011 at 12:56 AM

Aldonalettos work around will work for you but it may be another idea to give your projectile prefab a "projectile" tag and compare the collided objects tag instead.

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
avatar image
0

Answer by jbickel · Aug 11, 2011 at 01:03 AM

I'm using the answer section instead of the comment section so I can format it for legibility.

@ aldonaletto - What you say makes perfect sense, but my projectiles still bounce off of my terrain. Here is the actual code as it exists right now...

var muzzleFlash : Transform; var turret : Transform; var mainGun : Transform; var recoil : boolean = false; var reload : int = 0; var fireSound : AudioClip; var projectile : Rigidbody;

function Update() { //are we shooting? var firing : float = Input.GetAxis("Fire");

if (firing != 0) {

//if the main gun is not in a state of recoil, then fire! if (recoil == false) {

//fire effects and sound Instantiate (muzzleFlash, transform.position, transform.rotation); recoil = true; audio.PlayOneShot(fireSound); mainGun.Translate (Vector3(0,0,-.07));

//make projectile var clone : Rigidbody;

     clone = Instantiate(projectile, transform.position, transform.rotation);
     clone.velocity = transform.TransformDirection (Vector3.forward * 50);
     clone.name = "clone";
     }
 }

//if in state of recoil, recover if (recoil == true) {

 mainGun.Translate (Vector3(0,0,.0014));
 reload += 1;
 if (reload == 50) {
     reload = 0;
     recoil = false;
     }
 }

}

Second script is still...

function OnCollisionEnter (other : Collision) {

 if (other.name == "clone") {
     Destroy (other.gameObject);
     }
 }

Also, I just noticed this error...

Script error: OnCollisionEnter This message parameter has to be of type: Collision The message will be ignored.

What does it mean? Google and answers search keep mentioning "Make sure you use Collision instead of Collider".

Comment
Add comment · Show 4 · 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 Blankzz · Aug 11, 2011 at 01:10 AM 0
Share

read this http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionEnter.html

function OnCollisionEnter (other : Collision) {

 if (other.gameObject.name == "clone") {
     Destroy (other.gameObject);
     }
 }
avatar image Eric5h5 · Aug 11, 2011 at 01:44 AM 0
Share

Please don't post comments as answers--you can format code the same in comments. (Although it's easier in answers, so you can write as an answer, then copy and paste it to a comment.)

avatar image jbickel · Aug 11, 2011 at 01:49 AM 0
Share

I had copy/pasted the wrong script. I did have it as (other : Collision) in the actual script. However, $$anonymous$$e was...

 Destroy (other.gameObject);

And yours was...

 Destroy (other.gameObject.name);

Which coincidentally worked.

The win on this one goes to aldonaletto & Blankzz. Thanks for pointing out the answer to me.

@Eric - I didn't realize you could do that in comments until I saw Blankzz do it. Sorry, but this is only my third day on here.

avatar image aldonaletto · Aug 11, 2011 at 01:04 PM 0
Share

I missed this error: OnCollisionEnter receives a Collision object, not a Collider. @Blankzz is right: Collision.name doesn't exist, but Collision.gameObject.name is ok.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

col.gameObject.layer is not working 1 Answer

Unity creating static colliders 1 Answer

Does collider size affect performance? 1 Answer

How to prevent a rigidbody from pushing other rigidbodies? 1 Answer

My Objects are falling through the floor.. 0 Answers


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