Search Unity

Ignore Collision Issues

Discussion in 'Scripting' started by primoadamg, Aug 23, 2009.

  1. primoadamg

    primoadamg

    Joined:
    Aug 17, 2009
    Posts:
    98
    Alright I made a script to instantiate a projectile when I click and this is what I have now
    Code (csharp):
    1.  
    2. var GrenadeProjectile : Rigidbody;
    3. var Velocity = 200;
    4.  
    5. function Update () {
    6.    if (Input.GetButtonDown("Fire2")){
    7.        var Grenade : Rigidbody;
    8.        
    9.        Grenade = Instantiate(GrenadeProjectile, transform.position, transform.rotation);
    10.        
    11.        Grenade.velocity = transform.TransformDirection (Vector3.forward * Velocity);
    12.        
    13.        Physics.IgnoreCollision(GrenadeProjectile.collider, transform.root.collider, true);//transform.root.collider);
    14.     }
    15. }
    16.  
    However my IgnoreCollision statement doesn't seem to work I get an error message saying "Ignore Collision failed. Both colliders need to be activated when calling this ignore collision" It is between a half cylinder with a mesh collider and an average FirstPersonWalker Prefab everything I tried didn't work whenever I click the projectile explodes instantly obviously because its not ignoring collisions, so what am I doing wrong?
    any help would be appreciated, thanks
     
  2. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    It looks like you are calling ignore collision on your prefab and not the object you just instanced.
     
  3. primoadamg

    primoadamg

    Joined:
    Aug 17, 2009
    Posts:
    98
    I understand what your saying but how do you think I should go through fixing it? switching the IgnoreCollision Colliders?
     
  4. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    You can't call ignorecollision on a prefab, as a prefab by definition is not in your scene. That's why you're getting an error saying you passed it something not active. You want to pass it the collider attached to the game object you spawn, instead of it's non-instanced prefab.
     
  5. primoadamg

    primoadamg

    Joined:
    Aug 17, 2009
    Posts:
    98
    Alright I get it, but now I fixed that error I have a new one

    NullReferenceException: The prefab you want to instantiate is null.

    that is from the instantiated clone e.g

    Code (csharp):
    1.  
    2. var Projectile : Rigidbody;
    3. var Velocity = 200;
    4.  
    5. function Update () {
    6.    if (Input.GetButtonDown("Fire2")){
    7.        var Clone : Rigidbody;
    8.        
    9.        Clone = Instantiate(Projectile, transform.position, transform.rotation);
    10.        
    11.        Clone.velocity = transform.TransformDirection (Vector3.forward * Velocity);
    12.        
    13.        Physics.IgnoreCollision(Projectile.collider, transform.root.collider);
    14.     }
    15. }
    16.  
    Now its saying that the Clone variable is null but I didnt have this problom before...
    now what did I do wrong?
     
  6. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    You didn't drag your prefab to the appropriate variable in the inspector. You also still aren't quite getting the ignore collision thing. You still have the prefab in there.
     
  7. primoadamg

    primoadamg

    Joined:
    Aug 17, 2009
    Posts:
    98
    Okay... I'm clueless then :? any help?
     
  8. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    Code (csharp):
    1.       Clone = Instantiate(Projectile, transform.position, transform.rotation);
    2.  
    If you take a look at the above line where you instance your prefab...

    Clone - the new object you create an instance of.
    Projectile - the prefab that acts as a template for the object you create.

    Code (csharp):
    1.  
    2. Physics.IgnoreCollision(Projectile.collider, transform.root.collider);
    3.  
    You're still telling the physics engine to ignore the prefab's collider which obviously doesn't make sense, as the prefab is not part of the scene.
     
  9. primoadamg

    primoadamg

    Joined:
    Aug 17, 2009
    Posts:
    98
    Alright I retraced my steps and thanks to your assessment I fixed my mistake however after fixing it a previously working script an explosion script doesn't seem to instantiate the explosion effect on collision the projectile disappears though, but no explosion... It was working before. Here is the script for that component...
    Code (csharp):
    1.  
    2. var Explosion : GameObject;
    3.  
    4. function OnCollisionEnter( collision : Collision ){
    5. var contact : ContactPoint = collision.contacts[0];
    6.  
    7. var rotation = Quaternion.FromToRotation(Vector3.up, contact.normal);
    8. var instantiatedExplosion : GameObject = Instantiate(Explosion, contact.point, rotation);
    9.  
    10. Destroy( gameObject );
    11. }
    12.  
    if you could help me out one more time? :D
    Thank you for your help before though.
     
  10. primoadamg

    primoadamg

    Joined:
    Aug 17, 2009
    Posts:
    98
    Alright I take that back It was instantiating the explosion I just didn't pay enough attention to what was in the variable place... :/ thanks anyways, Quietus