# Chapter 8 - Time slows when marine dies

**URL:** <https://forums.kodeco.com/t/chapter-8-time-slows-when-marine-dies/39391>\
**Category:** Unity Games by Tutorials\
**Created:** [February 12, 2018, 7:34pm UTC](https://forums.kodeco.com/t/chapter-8-time-slows-when-marine-dies/39391 "2018-02-12T19:34:21Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dragondave64](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@dragondave64](https://forums.kodeco.com/u/dragondave64)\
**Post date:** [February 12, 2018, 7:34pm UTC](https://forums.kodeco.com/t/chapter-8-time-slows-when-marine-dies/39391/1 "2018-02-12T19:34:21Z")

</div>

I completed chapter 8 and have an issue. When the marine dies, his head pops off and the particle effects work. But the marine body slowly tumbles away and the projectiles keep on spawning. There is an object reference error: NullReferenceException: Object reference not set to an instance of an object  
Gun.fireBullet () (at Assets/Scripts/Gun.cs:49) I have checked the gravity in the project settings, but it seems gravity has no effect as it is set to Y = -900 which should cause the head and body to stop right where it is. I even started a new project and copied all the folders from the final folder in the tutorials source code. I get exactly the same result. Everything is going in slow motion once the marine dies. If I am not shooting and the marine dies, the body still tumbles away, but there is no object reverence error for fireBullet. I also noticed that the isDead variable in PlayerController.cs is never used.

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [February 12, 2018, 11:27pm UTC](https://forums.kodeco.com/t/chapter-8-time-slows-when-marine-dies/39391/2 "2018-02-12T23:27:14Z")

</div>

@vegetarianzombie @bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![bdmoakley](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bdmoakley/32/108772_2.png) [@bdmoakley](https://forums.kodeco.com/u/bdmoakley)\
**Post date:** [February 13, 2018, 3:00pm UTC](https://forums.kodeco.com/t/chapter-8-time-slows-when-marine-dies/39391/3 "2018-02-13T15:00:04Z")

</div>

Sounds like a few things are going on. The first thing I’d do is figure out the null reference exception. Don’t worry about the gravity or the isDead variable not being used. You’ll want to check if a GameObject is assigned to a property in the Gun script.

Let me know once you have the working and we can walk through the others. Errors have a way of cascading. One error can create other errors, so it’s good to start at the top and work them all out one at a time.

---

<div class="post-metadata">

**Author:** ![dragondave64](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@dragondave64](https://forums.kodeco.com/u/dragondave64)\
**Post date:** [February 13, 2018, 4:55pm UTC](https://forums.kodeco.com/t/chapter-8-time-slows-when-marine-dies/39391/4 "2018-02-13T16:55:21Z")

</div>

I confirmed that the Projectile prefab is in the bullet prefab property of the gun script. There is no reference error being thrown until the marine dies. I have confirmed that the final chapter 8 project from your source files does exactly the same thing. No edits. Also to note: the bullet prefab property of the gun script in the final project was empty. Adding the Projectile prefab to it had no effect. Could you reproduce on your end since it happens with the unchanged code from your package?

---

<div class="post-metadata">

**Author:** ![dragondave64](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@dragondave64](https://forums.kodeco.com/u/dragondave64)\
**Post date:** [February 13, 2018, 6:02pm UTC](https://forums.kodeco.com/t/chapter-8-time-slows-when-marine-dies/39391/5 "2018-02-13T18:02:15Z")

</div>

Note: I did change the total number of aliens to 50 to allow the marine to die while firing.

---

<div class="post-metadata">

**Author:** ![dragondave64](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@dragondave64](https://forums.kodeco.com/u/dragondave64)\
**Post date:** [February 15, 2018, 9:02pm UTC](https://forums.kodeco.com/t/chapter-8-time-slows-when-marine-dies/39391/6 "2018-02-15T21:02:10Z")

</div>

So I was able to work around this by adding a CancelInvoke command to the gun component in the Die method. The marine still spins away but the object reference error goes away and the remaining aliens cluster around the marine’s last alive position.

public void Die()  
{  
bodyAnimator.SetBool(“IsMoving”, false);  
marineBody.transform.parent = null;  
marineBody.isKinematic = false;  
marineBody.useGravity = true;  
marineBody.gameObject.GetComponent().enabled = true;  
marineBody.gameObject.GetComponent().enabled = false;

```
    marineBody.gameObject.GetComponent<Gun>().CancelInvoke();

    Destroy(head.gameObject.GetComponent<HingeJoint>());

    head.transform.parent = null;
    head.useGravity = true;

    SoundManager.Instance.PlayOneShot(SoundManager.Instance.marineDeath);

    deathParticles.Activate();

    isDead = true;

    Destroy(gameObject);
}

```

---

<div class="post-metadata">

**Author:** ![dragondave64](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@dragondave64](https://forums.kodeco.com/u/dragondave64)\
**Post date:** [February 16, 2018, 9:58pm UTC](https://forums.kodeco.com/t/chapter-8-time-slows-when-marine-dies/39391/7 "2018-02-16T21:58:10Z")

</div>

That didn’t paste well. Here it is.

public void Die()  
{  
bodyAnimator.SetBool(“IsMoving”, false);  
marineBody.transform.parent = null;  
marineBody.isKinematic = false;  
marineBody.useGravity = true;  
marineBody.gameObject.GetComponent().enabled = true;  
marineBody.gameObject.GetComponent().enabled = false;

```
    marineBody.gameObject.GetComponent<Gun>().CancelInvoke();
    
    Destroy(head.gameObject.GetComponent<HingeJoint>());
    head.transform.parent = null;
    head.useGravity = true;
    SoundManager.Instance.PlayOneShot(SoundManager.Instance.marineDeath);
    deathParticles.Activate();
    Destroy(gameObject);
}

```

---

<div class="post-metadata">

**Author:** ![nick2905](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@nick2905](https://forums.kodeco.com/u/nick2905)\
**Post date:** [March 19, 2018, 10:51am UTC](https://forums.kodeco.com/t/chapter-8-time-slows-when-marine-dies/39391/8 "2018-03-19T10:51:18Z")

</div>

Hi,  
I stopped the projectiles spawning by adding the following code at the beginning of the fireBullet() method, but after the bullet is created. Hope this helps.

if (!transform.parent)  
{  
return;  
}  
This also cures the null reference problem. I believe that the null reference is caused by the fact that the marine no longer exists, therefore the parent reference is null.

Sorry doesn’t look as if this works

Regards,  
Nick

---

<div class="post-metadata">

**Author:** ![nick2905](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@nick2905](https://forums.kodeco.com/u/nick2905)\
**Post date:** [March 19, 2018, 11:15am UTC](https://forums.kodeco.com/t/chapter-8-time-slows-when-marine-dies/39391/9 "2018-03-19T11:15:28Z")

</div>

Hello,

Sorry for the errors in the above post. I have now amended my code as follows. At the beginning of the fireBullet() method I have added the following

if (transform.parent == null)  
{  
return;  
}

Regards,  
Nick

---

<div class="post-metadata">

**Author:** ![dragondave64](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@dragondave64](https://forums.kodeco.com/u/dragondave64)\
**Post date:** [March 19, 2018, 1:04pm UTC](https://forums.kodeco.com/t/chapter-8-time-slows-when-marine-dies/39391/10 "2018-03-19T13:04:40Z")

</div>

Thanks, I was beginning to think no one else had the same issue.

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [July 29, 2018, 11:43am UTC](https://forums.kodeco.com/t/chapter-8-time-slows-when-marine-dies/39391/11 "2018-07-29T11:43:40Z")

</div>

Closed on July 29, 2018, 11:43am UTC

This topic was automatically closed after 166 days. New replies are no longer allowed.
