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

Sound won't play upon collision?

Discussion in 'Scripting' started by wikunia716, Jun 20, 2022.

  1. wikunia716

    wikunia716

    Joined:
    Jun 20, 2022
    Posts:
    1
    I tried to code this so that when the item is picked up it makes noise, but it just won't do it. Is it the coding that's the issue, if so, how do I fix it?

    using UnityEngine;
    using UnityEngine.Events;
    namespace Gamekit2D
    {

    public class InteractOnTrigger2D : MonoBehaviour
    {
    public LayerMask layers;
    public UnityEvent OnEnter, OnExit;
    public InventoryController.InventoryChecker[] inventoryChecks;
    protected Collider2D m_Collider;
    void Reset()
    {
    layers = LayerMask.NameToLayer("Everything");
    m_Collider = GetComponent<Collider2D>();
    m_Collider.isTrigger = true;
    }
    void OnTriggerEnter2D(Collider2D other)
    {
    if(!enabled)
    return;

    if (layers.Contains(other.gameObject))
    {
    ExecuteOnEnter(other);
    }
    }
    void OnTriggerExit2D(Collider2D other)
    {
    if(!enabled)
    return;

    if (layers.Contains(other.gameObject))
    {
    ExecuteOnExit(other);
    }
    }
    protected virtual void ExecuteOnEnter(Collider2D other)
    {
    OnEnter.Invoke();
    for (int i = 0; i < inventoryChecks.Length; i++)
    {
    inventoryChecks.CheckInventory(other.GetComponentInChildren<InventoryController>());
    }
    }
    protected virtual void ExecuteOnExit(Collider2D other)
    {
    OnExit.Invoke();
    }
    void OnDrawGizmos()
    {
    Gizmos.DrawIcon(transform.position, "InteractionTrigger", false);
    }
    }
    }
     
  2. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    488
    Put your code in code tags. You will find that people might just ignore your post if you don't do that.
    At a glance, you have nothing in that script related to playing sounds - so what exactly is this meant to do?
     
  3. CodeKiwi

    CodeKiwi

    Joined:
    Oct 27, 2016
    Posts:
    119
    I'm not familiar with Gamekit2D but looking at the code I'm expecting that it probably has an audio source attached. Then OnEnter probably calls something like audioSource.PlayOneShot() in the editor (might also play particles etc). I'd recommend checking how an existing prefab is setup. Capture.PNG
     
  4. bplc

    bplc

    Joined:
    Mar 10, 2022
    Posts:
    104
    I think this script will help you to understand, of course it will have to be modified a bit


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class monScript: MonoBehaviour
    4. {
    5.  
    6. private AudioSource playerAudio;
    7. public AudioClip mySound;
    8.  
    9.   void Start()
    10.     {
    11.  
    12.         playerAudio = GetComponent<AudioSource>();
    13.    
    14.     }
    15.  
    16.  
    17.     void Update()
    18.     {
    19.        if (Input.GetKeyDown(KeyCode.P)) //HERE touch P
    20. //normally it should play a sound when the P key is pressed, this is why it will be necessary to add another parameter //with && which means "and", (Input.GetKeyDown(KeyCode.P)&& other parameter )
    21.            {
    22.     playerAudio.PlayOneShot(mySound);
    23.           }
    24.  
    25.     }
    26.  
    27. }
     
    Last edited: Jun 20, 2022
  5. max6151

    max6151

    Joined:
    Jun 16, 2022
    Posts:
    9
    How do I even add that stuff in? I don't think I should be scripting on this one, it should all be included.
     

    Attached Files:

  6. max6151

    max6151

    Joined:
    Jun 16, 2022
    Posts:
    9
    there's an example of a script on an interactable. I can't get the sound of the key to play on contact more than once.
     
  7. bplc

    bplc

    Joined:
    Mar 10, 2022
    Posts:
    104
    if you follow a unity learn tutorial could you give me the link.

    my example says that the sound is played if you press the P key (you have to add an audiosource component to object where the script is, otherwise you have to put the variable "public AudioSource playerAudio;" to manually select the audio source when the P key is pressed.


    Could you tell me which object, is which script?
     
  8. bplc

    bplc

    Joined:
    Mar 10, 2022
    Posts:
    104
    for the key object for example, just put your sound in -> inspector -> Clip -> of the script of the inventory item, the sound plays when your character picks up the key, once the key picked up the sound stop playing it's normal, I don't understand what you are trying to do.
     
    Last edited: Jun 20, 2022
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    How to report your problem productively in the Unity3D forums:

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

    This is a bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    You may edit your post above.


    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    You must find a way to get the information you need in order to reason about what the problem is.
     
    bplc likes this.
  10. bplc

    bplc

    Joined:
    Mar 10, 2022
    Posts:
    104
    Thanks Kurt, I really try my best to help but can't figure out what's wrong...
     
  11. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,302
    Doesn't the key disappear into your inventory after you have picked it up?
    How would you pick it up more than once?
    Are you saying that the first key you pick up makes the sound, but the second key you pick up doesn't?

    Unity has a video tutorial series on this kit, here she sets up how a key interactable works.
    You shouldn't have to do that, considering you supposedly were given a complete game and you are just doing the audio, that should all be there, but it's useful information, and it shows you what you should be expecting to see.

    Does the key prefab (the file in your interactables folder) have the inventory item script attached?

    Do the key gameobjects (In your scene, not your folder) have the inventory item script attached?

    If it's not on the prefab, but it's on the gameobjects then you will have to drag and drop your audioclip on each key gameobject in your scene.

    If it's on the prefab, then you should only have to drag and drop your clip onto the prefab, and those changes should be propagated to the gameobjects that come from that prefab




    Here is the playlist, a lot of it will have nothing to do with audio though

    https://www.youtube.com/playlist?list=PLX2vGYjWbI0TLTUXKtLCAtd-iuCO2-W9X
     
    Last edited: Jun 20, 2022
  12. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,302