Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Instantiated objects not playing audio like parent object is.

Discussion in 'Scripting' started by danmct1995, Jun 18, 2020.

  1. danmct1995

    danmct1995

    Joined:
    Apr 21, 2020
    Posts:
    70
    So I have a game where I want an object to make a sound upon collision. one of the powerups makes the items duplicate, but those instantiated objects do not inherit my "Paddle 1" property and therefore do not have sound of their own. How would I go about reworking my parent object to make the instantiated children contain audio. Also, the only thing stored in this property is a clamp for where the player can move, so I'm not sure why this is actually effecting a different objects audio. I'll attach the scripts below:


    Keyboard controls (what is not attaching to the instantiated objects, nor do I want it to, but is effecting my audio)

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class KeyboardControls : MonoBehaviour
    7. {
    8.     [SerializeField] float moveSpeed = 20f;
    9.     [SerializeField] float screenWidthInUnits = 16f;
    10.     [SerializeField] float minX = 1.5f;
    11.     [SerializeField] float maxX = 14.5f;
    12.     public float paddleP;
    13.  
    14.     void Update()
    15.     {
    16.         MovementAreaClamp();
    17.     }
    18.  
    19.     private void MovementAreaClamp()
    20.     {
    21.         float ballLink = paddleP / Screen.width * screenWidthInUnits;
    22.         Vector2 paddlePosition = new Vector2(ballLink, transform.position.y);
    23.         paddleP = gameObject.transform.position.x;
    24.  
    25.         var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
    26.         var newXPos = Mathf.Clamp(transform.position.x + deltaX, minX, maxX);
    27.         transform.position = new Vector2(newXPos, transform.position.y);
    28.     }
    29. }
    30.  
    31.  
    The Ball script (Where I want audio to play from upon collision)

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Ball : MonoBehaviour
    4. {
    5.     // config parameter
    6.     [SerializeField] KeyboardControls paddle1;
    7.     [SerializeField] float pushX = 0.1f;
    8.     [SerializeField] float pushY = 10f;
    9.     [SerializeField] AudioClip[] ballSounds;
    10.     [SerializeField] float randomFactor = 0.2f;
    11.     [SerializeField] float LowRandomFactor = -0.2f;
    12.     // state
    13.  
    14.  
    15.     // cached component references
    16.     AudioSource myAudioSource;
    17.     Vector2 paddleToTheBall;
    18.  
    19.     Rigidbody2D rb;
    20.  
    21.     private bool hasStarted = false;
    22.  
    23.     /* Variables over */
    24.  
    25.     void Start()
    26.     {
    27.         paddleToTheBall = transform.position - paddle1.transform.position;
    28.         myAudioSource = GetComponent<AudioSource>();
    29.  
    30.         // rigidbody to rb
    31.         rb = GetComponent<Rigidbody2D>();
    32.     }
    33.  
    34.     // Update is called once per frame
    35.     void Update()
    36.     { // ball lock & stick
    37.         if (!hasStarted)
    38.         {
    39.             LockBallToPaddle();
    40.             LaunchOnClick();
    41.         }
    42.     }
    43.  
    44.     private void LaunchOnClick()
    45.     {
    46.         if (Input.GetKeyDown("space"))
    47.         {
    48.             hasStarted = true;
    49.             GetComponent<Rigidbody2D>().velocity = new Vector2(pushX, pushY);
    50.         }
    51.     }
    52.  
    53.    private void LockBallToPaddle()
    54.     {
    55.         Vector2 paddlePos = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
    56.         transform.position = paddlePos + paddleToTheBall;
    57.     }
    58.  
    59.     private void OnCollisionEnter2D(Collision2D collision)
    60.     {
    61.       Vector2 velocityAdjustment = new Vector2
    62.             //X axis
    63.             (Random.Range(LowRandomFactor,randomFactor),
    64.             //Y axis
    65.             Random.Range(0, randomFactor));
    66.         if (hasStarted)
    67.         {
    68.             AudioClip clip = ballSounds[UnityEngine.Random.Range(0, ballSounds.Length)];
    69.             myAudioSource.PlayOneShot(clip);
    70.             rb.velocity += velocityAdjustment;
    71.         }
    72.     }
    73. }
     
  2. danmct1995

    danmct1995

    Joined:
    Apr 21, 2020
    Posts:
    70