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

Question NOOB needs help..

Discussion in 'Scripting' started by Bernardbear, May 15, 2024.

  1. Bernardbear

    Bernardbear

    Joined:
    Apr 19, 2020
    Posts:
    31
    Good day, I need help with this bit. In this script, a bullet(bottle) will be destroyed on collision etc.
    My problem is that on the last line where I instantiate,play audio and destroy the object everything works fine except the audio.Keep on saying that i can not play disabled audio source. The audio source is in a different empty game object. I also tried calling another scripts method in this script but still didn't work. Not sure what to look or try .



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BulletBottle : MonoBehaviour
    6. {
    7.     public GameObject milk;
    8.     public AudioSource MilkySplash;
    9.    
    10.    
    11.    
    12.  
    13.     private void OnCollisionEnter(Collision collision)
    14.     {
    15.         if (collision.gameObject.TryGetComponent<BABYhEALTH>(out BABYhEALTH enemyComponent))
    16.         {
    17.             enemyComponent.TakeDamage(1);
    18.             Instantiate(milk, transform.position, Quaternion.identity);
    19.            
    20.         }
    21.        
    22.         Instantiate(milk, transform.position, Quaternion.identity);
    23.         MilkySplash.Play();
    24.         Destroy(gameObject);
    25.        
    26.  
    27.  
    28.  
    29.     }
    30.    
    31.  
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,988
    Is the audiosource enabled?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,364
    You're probably trying to play a prefab, not the instance in the scene.
     
  4. Bernardbear

    Bernardbear

    Joined:
    Apr 19, 2020
    Posts:
    31
    yes it is
     
  5. Bernardbear

    Bernardbear

    Joined:
    Apr 19, 2020
    Posts:
    31
    The Audio source is connected to an empty gameobject that is in the hierachy.
     
  6. Bernardbear

    Bernardbear

    Joined:
    Apr 19, 2020
    Posts:
    31
    Okay, i think I have an idea on whats going on.I couldnt link the empty gameobject to the prefab thats being called so I made a prefab out of the empty gameobject and dragged that into the prefab with the script above. So its calling the audio source from a prefab, but how do I link a gameobject in the hierachy to a prefab?
     
  7. Bernardbear

    Bernardbear

    Joined:
    Apr 19, 2020
    Posts:
    31
    I found a solution. I instantiate the audio source in this script. Then I will just write another script on the empty gameobject to destroy itself within 3s.