[SOLVED]Ignoring collision between colliders

Updated on May 15, 2016 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
5 on May 5, 2016

Hi everyone,

In a 2D game, I am spawning bullets with a circle collider 2D at the centre of a turret in order to shoot them. However, the turret also has a circle collider all around it, enclosing the bullets when they spawn. I think this is the reason why some bullets go in a different direction than expected and seem behaving unexpectedly. Thus I decided to apply this script to my turret:

 using UnityEngine;
 using System.Collections;
 public class AvoidImpactTurretRedbullets : MonoBehaviour {
 public GameObject redBulletPrefab;
 void Start () {
 Physics.IgnoreCollision (this.gameObject.GetComponent (), redBulletPrefab.GetComponent ());
 }
 }
 

Unfortunately I get the two following errors:

– The best overloaded method match for `UnityEngine.Physics.IgnoreCollision(UnityEngine.Collider, UnityEngine.Collider)’ has some invalid arguments

– Argument `#1′ cannot convert `UnityEngine.CircleCollider2D’ expression to type `UnityEngine.Collider’

Then I changed “CircleCollider2D” into “Collider” and no errors appeared anymore. But just when I spawned the turret, the game crashed and the following error came up:

– There is no ‘Collider’ attached to the “BulletRed” game object, but a script is trying to access it…. etc.

Any ideas? Thx!

  • Liked by
Reply
1 on May 5, 2016

I think you should take a look into layers and layer masks just search for it on the Unity manual

Helpful
on May 5, 2016

Alright, will do, thx

Show more replies
  • Liked by
Reply
Cancel
2 on May 14, 2016

I looked into that, but right now I would like to get this into working: 

Physics.IgnoreCollision (this.gameObject.GetComponent<Collider> (), redBulletPrefab.GetComponent<Collider> () )

Unity cannot take “CircleCollider2D” or even “Collider2D” instead of “Collider” in the getcomponent operator. Why is that? It does not want them, and when I try to access them it sends an error stating “no colliders are attached to the game objects i am trying to access..”, which is obvious since they are not 3D colliders, but 2D ones. It’s keep contradicting itself for some reason.

Guru
on May 15, 2016

Try changing it to:

Physics2D.IgnoreCollision (this.gameObject.GetComponent<CircleCollider2D>(), redBulletPrefab.GetComponent<CircleCollider2D>());

I assumed they were both CircleCollider2D if not change it with the right one.

Helpful
on May 15, 2016

It is working now apparently, my bad. Thanks!!

Show more replies
  • Liked by
Reply
Cancel