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

Gun Sound not working (Space Shooter Tutorial)

Discussion in 'Audio & Video' started by slethikk, Aug 17, 2016.

  1. slethikk

    slethikk

    Joined:
    Aug 17, 2016
    Posts:
    3
    Hello all, I'm fairly new to coding and game design and have been working through some of the tutorials. I've figured out almost everything except getting the audio for each gunshot. (audio for destroyed astroids and explosions working fine)

    Error Message:
    NullReferenceException: Object reference not set to an instance of an object
    PlayerController.Update () (at Assets/Scripts/PlayerController.cs:32)


    Can't seem to see what's causing the bug. Any help would be appreciated.

    Script :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. [System.Serializable]
    4. public class Boundary
    5. {
    6.     public float xMin, xMax, zMin, zMax;
    7. }
    8.  
    9. public class PlayerController : MonoBehaviour
    10. {
    11.     public Rigidbody rb;
    12.     public float speed;
    13.     public Boundary boundary;
    14.     public float tilt;
    15.     public GameObject shot;
    16.     public Transform ShotSpawn;
    17.     public float fireRate;
    18.     private float nextFire;
    19.     private AudioSource audioSource;
    20.  
    21.  
    22.     void start (){
    23.         rb = GetComponent <Rigidbody>();
    24.         audioSource = GetComponent <AudioSource>();
    25.     }
    26.  
    27.     void Update ()
    28.     {
    29.         if (Input.GetButton ("Fire1") && Time.time > nextFire) {
    30.             nextFire = Time.time + fireRate;
    31.             Instantiate (shot, ShotSpawn.position, ShotSpawn.rotation);
    32.             audioSource.Play ();
    33.         }
    34.         }
    35.  
    36.         void FixedUpdate ()
    37.     {
    38.         float moveHorizontal = Input.GetAxis ("Horizontal");
    39.         float moveVertical = Input.GetAxis ("Vertical");
    40.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    41.         rb.velocity = movement * speed;
    42.         rb.position = new Vector3
    43.             (
    44.                 Mathf.Clamp (rb.position.x, boundary.xMin, boundary.xMax),
    45.                 0.0f,
    46.                 Mathf.Clamp (rb.position.z, boundary.zMin, boundary.zMax)
    47.             );
    48.         rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);
    49.     }
    50. }
     
  2. rorywalsh

    rorywalsh

    Joined:
    Apr 10, 2015
    Posts:
    114
    Are you sure you've added an AudioSource component to your game object? But I think the problem might actually be your shot object. Are you absolutely certain you've dragged an instance of your 'shot' gameobject to the public GameObject shot in your inspector? I can't tell you how many times I've done this in the past!
     
  3. slethikk

    slethikk

    Joined:
    Aug 17, 2016
    Posts:
    3
    I think you're onto something. I hadn't included the audio in the shot prefab. Still getting same error after I did it, though. :(
     
  4. rorywalsh

    rorywalsh

    Joined:
    Apr 10, 2015
    Posts:
    114
    I'm getting a sense of something not quite right here. Are you trying to play a sound that is part of your shot prefab, or part of your PlayerController script? If you are trying to play a sound that is part of your shot prefab you should probably get the AudioSource component from your shot prefab, and not your PlayerController:

    Code (CSharp):
    1. void start (){
    2.         rb = GetComponent <Rigidbody>();
    3.         audioSource = shot.GetComponent <AudioSource>();
    4.     }
    5.  
    Does this make sense?
     
  5. slethikk

    slethikk

    Joined:
    Aug 17, 2016
    Posts:
    3
    It does but I'm still struggling with it. Oh the joys of coding
     
  6. rorywalsh

    rorywalsh

    Joined:
    Apr 10, 2015
    Posts:
    114
    So does it work now when you try to access the shot prefab's AudioSource?