Audio problems in game. Part 26 on how to make a 2d platformer

Updated on December 21, 2016 in [A] Audio
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
3 on December 21, 2016

hi,

So I got my audio working fine and dandy. If my player jumps and hits the ground a little poof can be heared. But when I respawn with my player it doesn’t play the sound anymore :/.
Strange thing is that it does play all the other sounds that are linked with the player.

for instance: His grunt voice, death voice and shooting etc. I have no idea why it won’t play the landing sound. I’ve tried checking my player (clone) but it seems that that works alright as well with the different audio settings.

I get the error messages: NullReferenceException: Object reference not set to an instance of an object. when trying to get the jump sound working. this error leads me back towards this piece of code:

                audioManager.PlaySound(landingSoundName);

Strange thing is that I implanted all the other sounds the same way and unity has no problem with that whatshowever.

Does anybody know a solution or a tip for where to look. I have no clue because I am an absolute beginner with coding.

this is the whole string of code where the audio is implanted:

 using System;
 using UnityEngine;
 namespace UnityStandardAssets._2D
 {
 public class PlatformerCharacter2D : MonoBehaviour
 {
 [SerializeField] private float m_MaxSpeed = 10f; // The fastest the player can travel in the x axis.
 [SerializeField] private float m_JumpForce = 400f; // Amount of force added when the player jumps.
 [Range(0, 1)] [SerializeField] private float m_CrouchSpeed = .36f; // Amount of maxSpeed applied to crouching movement. 1 = 100%
 [SerializeField] private bool m_AirControl = false; // Whether or not a player can steer while jumping;
 [SerializeField] private LayerMask m_WhatIsGround; // A mask determining what is ground to the character
 [SerializeField]
 string landingSoundName = "LandingFootsteps";
 private Transform m_GroundCheck; // A position marking where to check if the player is grounded.
 const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded
 private bool m_Grounded; // Whether or not the player is grounded.
 private Transform m_CeilingCheck; // A position marking where to check for ceilings
 const float k_CeilingRadius = .01f; // Radius of the overlap circle to determine if the player can stand up
 private Animator m_Anim; // Reference to the player's animator component.
 private Rigidbody2D m_Rigidbody2D;
 private bool m_FacingRight = true; // For determining which way the player is currently facing.
 public GameObject playerObject;
 private Transform PlayerGraphics;
 AudioManager audioManager;
 private void Awake()
 {
 // Setting up references.
 m_GroundCheck = transform.Find("GroundCheck");
 m_CeilingCheck = transform.Find("CeilingCheck");
 m_Anim = GetComponent();
 m_Rigidbody2D = GetComponent();
 PlayerGraphics = transform.FindChild("Graphics");
 if (PlayerGraphics == null)
 {
 Debug.LogError("Let's Freak Out");
 }
 }
 void start ()
 {
 audioManager = AudioManager.instance;
 if (audioManager == null)
 {
 Debug.LogError("freak out no audiomanager");
 }
 }
 private bool inAir = false;
 private void FixedUpdate()
 {
 m_Grounded = false;
 // The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
 // This can be done using layers instead but Sample Assets will not overwrite your project settings.
 Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
 for (int i = 0; i < colliders.Length; i++)
 {
 if (colliders[i].gameObject != gameObject)
 m_Grounded = true;
 }
 if (!m_Grounded && !inAir)
 {
 inAir = true;
 }
 m_Anim.SetBool("Ground", m_Grounded);
 if (m_Grounded && inAir)
 {
 inAir = false;
 audioManager.PlaySound(landingSoundName);
 }
 // Set the vertical animation
 m_Anim.SetFloat("vSpeed", m_Rigidbody2D.velocity.y);
 }
 

Help would be really appriciated 🙂
I’m really sorry if this is an easy fix that I looked over.

  • Liked by
Reply
2 on December 21, 2016

That seems strange. I feel your frustration.
 
The only thing that sort of seems different than the other variables around it is, maybe put ‘private’ before ‘string landingSoundName’. I don’t see why this would really change anything though.
 
I haven’t watched that video, and I don’t know how the audioManager works exactly, but it seems that this script is having difficulties finding which sound to actually play. Perhaps the name is spelled slightly differently, or the AudioManager script needs to also contain the audio file you want… etc. 
 
Maybe you could post that script info as well?

on December 21, 2016

Holy sh*t I have found a solution 😀

I’ve made this part of the code public

public Audiomanager audioManager;

and dragged my audiomanager object into the player object and now it works!

I really appriciated that you helped me 🙂 I will certainly remember your tips for upcoming bugs that will happen haha.

thanks mate

on December 21, 2016

I should not have cheered so early 🙁 when my player respawns I get the same error again. 

Don’t know if I should check my game master script or player now. My respawn code is made in my gamemaster script.

Show more replies
  • Liked by
Reply
Cancel