Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help on my 2D space shooter game (shooting the laser)

Discussion in 'Scripting' started by Jrchurch25, Nov 9, 2020.

  1. Jrchurch25

    Jrchurch25

    Joined:
    Jun 24, 2020
    Posts:
    1
    Hey there,

    First I want to apologize, as I am new to Unity and new to C# coding. For anyone willing to help this newb, I greatly appreciate it!

    So, so far, I've got the ship moving and many facets working the way they should: The rigidbody, movement, damage, box collider, etc... I even have the laser shooting. However, it automatically shoots at the beginning (not supposed to happen), then it is destroyed (as it is supposed to) once it is off the screen. I can shoot the laser as many times as I want... UNTIL the first laser is destroyed, then it glitches and ends the game, giving me an error:

    "MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object.
    "

    Having said that (and not knowing where the problem is) I will post the C# code I wrote for the Laser, as well as the SelfDestruct, WeaponMovement, and Damage. And again, I greatly appreciate anyone that is able to help. Let me know if I'm missing anything, or anything you may need in order to help me out.

    To sum up:

    1) I need help to get the laser to stop shooting on its own at the beginning.
    2) I need to destroy the laser once off screen; however, I need the ability to continue shooting the laser at any time (for obvious reasons...)


    Laser:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Laser : MonoBehaviour
    5. {
    6.  
    7.     public Vector3 bulletOffset = new Vector3(0, 0.5f, 0);
    8.  
    9.     public GameObject bulletPrefab;
    10.     int bulletLayer;
    11.  
    12.     public float fireDelay = 0.5f;
    13.     float cooldownTimer = 0;
    14.  
    15.     void Start()
    16.     {
    17.         bulletLayer = gameObject.layer;
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         cooldownTimer -= Time.deltaTime;
    24.  
    25.         if (Input.GetButton("Laser") && cooldownTimer <= 0)
    26.         {
    27.             // SHOOT!
    28.             cooldownTimer = fireDelay;
    29.  
    30.             Vector3 offset = transform.rotation * bulletOffset;
    31.  
    32.             GameObject bulletGO = (GameObject)Instantiate(bulletPrefab, transform.position + offset, transform.rotation);
    33.             bulletGO.layer = bulletLayer;
    34.  
    35.         }
    36.  
    37.     }
    38. }

    SelfDestruct:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SelfDestruct : MonoBehaviour
    5. {
    6.  
    7.     public float timer = 3f;
    8.  
    9.     void Update()
    10.     {
    11.         timer -= Time.deltaTime;
    12.  
    13.         if (timer <= 0)
    14.         {
    15.             Destroy(gameObject);
    16.         }
    17.     }
    18. }

    WeaponMovement:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class weaponmovement : MonoBehaviour
    5. {
    6.  
    7.     public float maxSpeed = 5f;
    8.  
    9.     // Update is called once per frame
    10.     void Update()
    11.     {
    12.         Vector3 pos = transform.position;
    13.  
    14.         Vector3 velocity = new Vector3(0, maxSpeed * Time.deltaTime, 0);
    15.  
    16.         pos += transform.rotation * velocity;
    17.  
    18.         transform.position = pos;
    19.     }
    20. }

    Damage:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class damage : MonoBehaviour
    5. {
    6.  
    7.     public int health = 1;
    8.  
    9.     public float invulnPeriod = 0;
    10.     float invulnTimer = 0;
    11.     int correctLayer;
    12.  
    13.     SpriteRenderer spriteRend;
    14.  
    15.     void Start()
    16.     {
    17.         correctLayer = gameObject.layer;
    18.  
    19.         // NOTE!  This only get the renderer on the parent object.
    20.         // In other words, it doesn't work for children. I.E. "enemy01"
    21.         spriteRend = GetComponent<SpriteRenderer>();
    22.  
    23.         if (spriteRend == null)
    24.         {
    25.             spriteRend = transform.GetComponentInChildren<SpriteRenderer>();
    26.  
    27.             if (spriteRend == null)
    28.             {
    29.                 Debug.LogError("Object '" + gameObject.name + "' has no sprite renderer.");
    30.             }
    31.         }
    32.     }
    33.  
    34.     void OnTriggerEnter2D()
    35.     {
    36.         health--;
    37.  
    38.         if (invulnPeriod > 0)
    39.         {
    40.             invulnTimer = invulnPeriod;
    41.             gameObject.layer = 10;
    42.         }
    43.     }
    44.  
    45.     void Update()
    46.     {
    47.  
    48.         if (invulnTimer > 0)
    49.         {
    50.             invulnTimer -= Time.deltaTime;
    51.  
    52.             if (invulnTimer <= 0)
    53.             {
    54.                 gameObject.layer = correctLayer;
    55.                 if (spriteRend != null)
    56.                 {
    57.                     spriteRend.enabled = true;
    58.                 }
    59.             }
    60.             else
    61.             {
    62.                 if (spriteRend != null)
    63.                 {
    64.                     spriteRend.enabled = !spriteRend.enabled;
    65.                 }
    66.             }
    67.         }
    68.  
    69.         if (health <= 0)
    70.         {
    71.             Die();
    72.         }
    73.     }
    74.  
    75.     void Die()
    76.     {
    77.         Destroy(gameObject);
    78.     }
    79.  
    80. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Gather the value
    Input.GetButton("Laser")
    to a temporary boolean and use Debug.Log() to print it out. It must be true when you don't think it is, or else something else is making your bullet.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    One way is to put an object just barely off the top of the screen and give the bullet a reference to that object. If the bullet's position ever goes north of that object, Destroy() it.
     
  5. VishwasGagrani

    VishwasGagrani

    Joined:
    May 12, 2018
    Posts:
    81
    Code (CSharp):
    1.  void Update()
    2.     {
    3.         timer -= Time.deltaTime;
    4.         if (timer <= 0)
    5.         {
    6.             Destroy(gameObject);
    7.         }
    8.     }
    After destroying gameObject once. You are destroying it many more times. You need to stop destroying the gameObject if it does not exist.