How to enable audio source in Unity? - c#

I am trying to add some sound to my unity practice project and I have applied the sound and prepared the script necessary for it... but now whenever I am testing it through Unity player audio is not working.. each time I am getting this error log.
"Can not play a disabled audio source UnityEngine.AudioSource:Play()
coinhandler:OnCollisionEnter2D(Collision2D) (at
Assets/Script/coin/coinhandler.cs:24)"
My Coinhandler.cs script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Audio;
public class coinhandler : MonoBehaviour {
public Transform particles;
public AudioSource coinCollectS;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D (Collision2D collider)
{
if (collider.gameObject.tag == "Player")
coinCollectS.Play ();
{
Instantiate (particles, new Vector3 (transform.position.x, transform.position.y, -0.2f), Quaternion.identity);
shooterMain.IncreaseSpeed ();
shooterMain.increasePoint ();
GameObject.FindWithTag ("points").GetComponent<Text>().text = shooterMain.getPoints ().ToString ();
Destroy (gameObject);
}
}
}

Related

Unity clone won't appear

I'm new to coding and i'm trying to spawn a clone of a ball in unity, but it won't appear.
It does spawn at the spawn point but it doesn't appear.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour
{
public GameObject ball;
public Transform spawnPoint;
// Start is called before the first frame update
void Start()
{
SpawnBall();
}
// Update is called once per frame
void Update()
{
}
void SpawnBall()
{
Instantiate(ball, spawnPoint.position, Quaternion.identity);
}
}
Edit: it's a 3d game and the mesh renderer is enabled.
Okay nevermind: i just redid the ball and it worked.
¯_(ツ)_/¯

Unity on trigger enter can't play animation

I'm a noob at c# in unity. I've been trying to trigger an animation "hit" when a rigid body enters a box collider set to Is Trigger. I have the animation clip, and the console shows me that I have "Entered Trigger". I can't seem to get my animation clip to play once this happens. By the way this is regarding 3D models. Can anyone help?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class newAttack : MonoBehaviour {
public GameObject tiger;
public AnimationClip attack;
private Animation myAnimation;
/* IEnumerator Wait()
{
myAnimation = GetComponent<Animation>();
myAnimation.Play(attack.name);
yield return new WaitForSeconds(3);
}
*/
void OnTriggerEnter(Collider other)
{
Debug.Log("Entered Trigger");
myAnimation = GetComponent<Animation>();
myAnimation.Play(attack.name);
// StartCoroutine(Wait());
}
// Use this for initialization
// Update is called once per frame
void Update () {
}
}
You can try to use the Animator and play the name of the animation (present in the animator).
The code would like:
myAnimation = GetComponent<Animator>();
myAnimation.Play(attack.name);
Ref: https://docs.unity3d.com/ScriptReference/Animator.Play.html

Cannot See Public Variable in Sidebar of Unity

I was using this tutorial to code a VR game in Unity. When I got to the scripting, I ran into an issue. I could not see variables in the Inspector sidebar. Here is my code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hello : MonoBehaviour {
[SerializeField]
public AudioClip clip;
public AudioSource audioSource;
public Transform gunBarrelTransform;
void Start ()
{
audioSource = GetComponent<AudioSource> ();
audioSource.clip = clip;
}
void Update (){
if (OVRInput.Get (OVRInput.Button.SecondaryHandTrigger)) {
audioSource.Play ();
RaycastGun ();
}
}
private void RaycastGun()
{
RaycastHit hit;
if (Physics.Raycast (gunBarrelTransform.position,
gunBarrelTransform.forward, out hit)) {
if (hit.collider.gameObject.CompareTag ("Cube")) {
Destroy (hit.collider.gameObject);
}
}
}
}
Even when I remove the [SerializeField] it does not work. Does anybody know why the variables aren't showing up? Thank you so much for the help!

Audio not playing when ball enters the trigger

i have a game like the Rooltheball in unitys tutorials, and i want to play a sound when my ball hits the peaks, the thing is that i already tried everything, i basicly added a audioListener in my mainCamera, and added a audioSource and audioClip in the gameobject i want to detect the trigger, here is the code i did:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class colisaoPicos : MonoBehaviour {
Manager gameManager;
public AudioClip impact;
private AudioSource audio;
void Start()
{
gameManager = GameObject.Find ("GameController").GetComponent<Manager> ();
}
void OnTriggerEnter(Collider c)
{
if (c.gameObject.tag == "Player") {
AudioSource.PlayClipAtPoint (impact, transform.position);
gameManager.LifeDown();
}
}
}
One solution would be to add an audiosource on the ball, and make it play the clip each time you enter the trigger.
Just make the audio variable public, and drag the AudioSource into it in the inspector.
Second, but in my book, and uglier solution would be to make a gameobject spesifically for that purpose. Then put the gameobject at the spot, and play the sound.
But, as programmer said, you should check the trigger. Remember, one of the Game Objects has to contain a Rigidbody. (Is Kinematic can be active tho)
As my experience, the following code is always working:
public AudioSource soundToPlay;
void OnTriggerEnter(Collider c)
{
if (c.gameObject.tag == "Player") {
soundToPlay.Play ();
}
}
Do not try to use playOneShot, or play clip. Error happens.
Try this it will play the sound once if Trigger function is working in your case and do add AudioListener component with the trigger gameobject.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class colisaoPicos : MonoBehaviour {
Manager gameManager;
public AudioClip impact;
private AudioSource audio;
void Start()
{
audio=GetComponent<AudioSource>();
gameManager = GameObject.Find ("GameController").GetComponent<Manager();
}
void OnTriggerEnter(Collider c)
{
if (c.gameObject.tag == "Player") {
audio.PlayOneShoot(impact);
gameManager.LifeDown();
}
}
}

Unity Collision Detection - Adding GUI Score on Collision?

I am making a pinball game in Unity, and I have an issue. When the pinball collides with a cylinder to add points to the score, it does not work. I have tagged the cylinders in Unity and have attached this script to the pinball. It doesn't even show up in the debug log.
Thanks for any advice.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Score : MonoBehaviour {
public int scorePoint = 10;
public int MaxScore;
public Text ScoreText;
// Use this for initialization
void Start () {
ScoreText = GetComponent<Text>();
ScoreText.text = "Score: " + scorePoint;
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == "Cylinder")
{
Debug.Log("Collision detected");
scorePoint+=10;
}
}
// Update is called once per frame
void Update()
{
}
}
Make sure you have a box collider on each object. OnTriggerEnter is only called when two box collider hit each other. This is the most likely culprit of why its not working but without more information I can't guarantee it.

Categories