Group Group Group Group Group Group Group Group Group

Chapter 8 - Time slows when marine dies

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.

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

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.

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?

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

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);
}

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);
}

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

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

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

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