- Home /
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.
i also checked the matrix and unchecked the thing correctly
i have made a picture of the scene now all errors are gone but it starts to make clones of clones which is not my purpose cause it's making my pc lagg hard
the thing that my player is holding is the floating stone with the spawn inside of it it wont shoot the stone tho but the clones
Answer by Bunny83 · Oct 21, 2015 at 02:09 PM
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.
i clone the instance that is already in my game also i think getcomponent is actually needed cause when i press the Fire1 button my player falls through the terrain i assigned my projectile object (the one that is also in my scene already) and i assigned a barrel to barrel which in this case is my spawn
I bet your projectile has hit the terrain? and destroyed the terrain collider? ^^ $$anonymous$$ake sure you setup the collision matix the right way. $$anonymous$$ake sure the projectile can only hit things that it is supposed to hit.
Also inside the projectile script, you should check what it actually hits. You often want that a projectile can hit the terrain, but you don't want the terrain to be destroyed. So make sure you handle the collision the right way.
The easiest way is to use another layer or a tag to "mark" objects that can be destroyed by the projectile so the script on the projectile can check the tag / layer before destroying objects.
Answer by allenallenallen · Oct 21, 2015 at 01:10 PM
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);
no it does not work without that line cause then it will shoot but my spawn (and projectile which is floating there also) will get hit by the just created clone and float away but if i put that line in my code it gives the error and won't instantiate anything
i deleted the getcomponent parts in my script but now my player just falls through the terrain when i press the "Fire1" button
Answer by yvyv1000 · Oct 22, 2015 at 08:09 AM
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 :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612042142im_/https://answers.unity.com/themes/thub/images/avi.jpg)