Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

3.x Game Development Book Scripting Problem, -door does not play sound on collision-

Discussion in 'Scripting' started by kenlyl, Oct 1, 2012.

  1. kenlyl

    kenlyl

    Joined:
    Oct 1, 2012
    Posts:
    2
    I am new to Unity and trying to work through Will Goldstone's book Unity 3.x Development Essentials. It has been a very informative book thus far and I have enjoyed the explanations and process.

    However, I am stuck part way through chapter 5 while trying to get a playercollider set up to activate a door sound and animation when the FPC collides with the box collider assigned to a door object.

    The setup is as follows:
    on the door object I have a box collider and an audio source.

    On the FPC I added the script below and assigned two audio files to the public variables for door open and close sound.

    I get no errors when saving the script, only the following warning " CS0414: The private field 'PlayerCollisions.doorTimer' is assigned but its value is never used

    Being totally new to scripting I can't seem to find any errors , at least that I can recognize, in the script. I went through the setup explanations in the book several times and can't seem to find anything that I missed or set up incorrectly either.

    Obviously I am missing something however because when I walk up to the door while testing the game no sounds play.

    Also the audio listener is on the FPC camera and I am hearing the ambient terrain sound looping w/o any issues.

    Any advice would be greatly appreciated, I was really hoping to solve this on my own, but my hope is quickly waning :-|

    Thanks in advance!

    PlayerCollisions script below that I am using:
    ----------------------------------------------------------------------------------------
    using UnityEngine;
    using System.Collections;

    public class PlayerCollisions : MonoBehaviour {
    bool doorIsOpen = false;
    float doorTimer = 0.0f;
    public float doorOpenTime = 3.0f;
    public AudioClip doorOpenSound;
    public AudioClip doorShutSound;

    // Use this for initialization
    void Start () {
    }
    // Update is called once per frame
    void Update () {
    }
    void OnControllerColliderhit(ControllerColliderHit hit) {
    if(hit.gameObject.tag == "playerDoor" doorIsOpen == false) {
    OpenDoor(hit.gameObject);
    }
    }
    void OpenDoor(GameObject door) {
    doorIsOpen = true;
    door.audio.PlayOneShot(doorOpenSound);

    }
    }

    ----------------------------------------------------------------------------------------------
     
  2. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    Correct the casing of the H in OnControllerColliderhit so it says OnControllerColliderHit
     
  3. kenlyl

    kenlyl

    Joined:
    Oct 1, 2012
    Posts:
    2
    Wow, ...what an obvious mistake, I can't believe I did not see that myself! I went over that script a dozen times looking for errors and never saw it!

    Thanks for your help!
     
  4. TylerCode_

    TylerCode_

    Joined:
    Sep 8, 2010
    Posts:
    221
    We've ALL done it before lol.
     
  5. Asii

    Asii

    Joined:
    Sep 28, 2012
    Posts:
    53
    next time you've got a problem use the
    Code (csharp):
    1. [/ CODE] much easier for people to help you :)