projectile must ignore collision with itself

i have this scirpt

 var projectile : Rigidbody;
 var speed = 100;
 var barrel : Transform;
 var shots :  int  =  0 ;
 var maxShots :  int  =  1 ;
 
 function Update () {
     fwd = transform.TransformDirection(Vector3.forward);
 
 if ( Input.GetButtonDown ("Fire1") && shots < maxShots){
        Shoot();
 }
 else if (shots >= maxShots)
     {
		Reload();
     }
 }
 
 function Shoot(){
         var bullet1 : Rigidbody;
	    Physics.IgnoreLayerCollision(8,8, true);
        bullet1 = Instantiate(projectile, barrel.position, barrel.rotation) as Rigidbody;
        bullet1.GetComponent(Rigidbody).useGravity = true;
        bullet1.GetComponent(Rigidbody).AddForce(barrel.forward * speed, ForceMode.Impulse);
 shots ++;
 }
 
 function Reload() {
  yield WaitForSeconds(0.5); 
         shots = 0;
 }

which will instantiate a projectile and fire it while ignoring it’ s own collsion
this is because my projectile will spawn inside itself in the game
if someone is going to say that i could just move the spawn out of the projectile my answer is
i can’t do that because of a animation i have for throwing the object (this animation is not in the script just yet ill implement it later)
so i used a ignorelayercollision to prevent it from colliding with himself
but whe i use it, it gives this error and wont spawn the clones

NullReferenceException: Object reference not set to an instance of an object
StoneFire.Shoot () (at Assets/MY Assets/Scripts/StoneFire.js:21)
StoneFire.Update () (at Assets/MY Assets/Scripts/StoneFire.js:11)

i have this same error on line 18 too but that one does not mess up my in game actions so i wont fix that now (if someone knows how to fix it you can say that too)
can someone please tell me why my projectile won’t spawn and give that error

projectile information:
it is a rigidbody
is kanematic is off
use gravity is off in editor but script enables it
uses sphere collider and not a mesh collider because then i had to put is kanematic on and that would lead to a not moving bullet.

Are you saying that the code works when you take out take out this line?

Physics.IgnoreLayerCollision(8,8, true);

Because I can see that it still wouldn’t work. Your “bullet1” variable is a Rigidbody. Which means you don’t need to get the Rigidbody components at all. You’re trying to get components that don’t exist: A rigidbody within a rigidbody.

bullet1.useGravity = true;
bullet1.AddForce(barrel.forward * speed, ForceMode.Impulse);

There are exactly two possible reasons for why you get a null reference exception inside Shoot:

  • Either you did not assign your projectile prefab to your projectile variable in the inspector
  • or you didn’t assign your barrel gameobject to the barrel variable in the inspector

The barrel can also be null when you somehow destroyed the gameobject.

As allen mentioned in his answer it’s pointless (though not an error) to use GetComponent(Rigidbody) since your bullet1 variable is already a rigidbody.

A rigidbody actually can’t collide with itself. Do you actually use prefab or do you clone an instance that is already in your game?

edit
Reading your comment above it seems that you indeed have an instance of your projectile already inside your scene. Is this some kind of rocket launcher? If so i would suggest:

  • make sure you have your rocket as prefab in your project
  • assign the prefab to the projectile variable
  • delete the projectile from your scene so you only have your weapon there without the projectile
  • add another variable that will hold the “current” loaded projectile
  • when you “reload” your weapon you actually instantiate a projectile at the barrel position and parent it to your weapon. Store the reference to that projectile inside the “current” variable. Now the weapon is loaded.
  • When you fire your weapon you simply check if “current” is not null and if so you simply unparent the “current” object, and launch it by setting useGravity = true and apply your launch force.
  • Once launched you want to set “current” to null.
  • You can’t fire again until a new projectil has been instantiated when you call reload

If it’s a “normal” weapon you should do:

  • make sure you have your bullet as prefab in your project
  • assign the prefab to the projectile variable
  • delete the projectile from your scene so you only have your weapon there without the projectile
  • when you fire your weapon you simply instantiate the prefab at the barrels position and launch it like you did already.

I guess your bullet has a script attached that destroy things it hits? Make sure you ignore collisions between the barrel and your projectile in case the barrel has a collider. If you don’t your projectile might destroy your barrel once you shoot.

fixed it with your help i made a prefab of the scene object and assigned that to my projectile var insteed of the scene object i added what mass and now its working like a charm i thank you
for everyone who wants to use my script here is the working one:

  var projectile : Rigidbody;
  var speed = 100;
  var barrel : Transform;
  var shots :  int  =  0 ;
  var maxShots :  int  =  1 ;
  
  function Update () {
      fwd = transform.TransformDirection(Vector3.forward);
  
  if ( Input.GetButtonDown ("Fire1") && shots < maxShots){
         Shoot();
  }
  else if (shots >= maxShots)
      {
         Reload();
      }
  }
  
  function Shoot(){
          var bullet1 : Rigidbody;
         Physics.IgnoreLayerCollision(8,8, true);
         bullet1 = Instantiate(projectile, barrel.position, barrel.rotation) as Rigidbody;
         bullet1.GetComponent(Rigidbody).useGravity = true;
         bullet1.GetComponent(Rigidbody).AddForce(barrel.forward * speed, ForceMode.Impulse);
  shots ++;
  }
  
  function Reload() {
   yield WaitForSeconds(0.5); 
          shots = 0;
  }

feel free to edit it or use it
2 things

make sure your projectile is a prefab
and if you dont need the layer part delete it to make the script less heavy

peace out :slight_smile: