c# - 无法播放禁用的音频源(Unity 2d)

标签 c# unity3d audio

我有一个小问题;我正在尝试开发自己的无尽跳跃游戏。这就是为什么我创建了一种可以发射小型弹丸的敌人类型。每次拍摄时,都应该播放声音,但不会播放。调试:“无法播放禁用的音频源。”

最初,这种方法在预制件上是行不通的,但最近在第一个原始对手上也行不通。 (我有点弄乱了代码中的内容)

每个提出的解决方案都会对我有很大帮助:)

我知道,那里有一些有关此主题的帖子,但是这些都没有帮助...

有点凌乱的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FlyingEnemy : MonoBehaviour {

public float startTimeBtwShots;
private float timeBtwShots;
private Vector3 spawnPoint;

public AudioSource shot;

public Animator animator;

private float flyingSpeed;
private float turningPoint;

private bool moving;

public GameObject projectile;

public GameObject player;

void Start () 
{
    turningPoint = Mathf.Abs(transform.position.x);
    flyingSpeed = 0.008f;
    timeBtwShots = startTimeBtwShots;
    moving = true;
    shot.enabled = true;
}

void Update () 
{
    //Ignore, moves enemy from left to right and back 
    if (moving)
    {
        transform.position = new Vector3(transform.position.x + flyingSpeed, transform.position.y, transform.position.z);
    }
    if (transform.position.x >= turningPoint || transform.position.x <= turningPoint * -1)
    {
        flyingSpeed *= -1;
        transform.position = new Vector3(transform.position.x + flyingSpeed, transform.position.y, transform.position.z);
        if (flyingSpeed <= 0) transform.eulerAngles = new Vector3(0, 180, 0);
        else if (flyingSpeed >= 0) transform.eulerAngles = new Vector3(0, 0, 0);
    }

    //Important part: projectile gets spawn and a bit earlier the sound should be played...
    if (timeBtwShots <= 0)
    {
        spawnPoint = transform.position;
        spawnPoint.y -= 0.35f; 
        Instantiate(projectile, spawnPoint, Quaternion.identity);
        timeBtwShots = startTimeBtwShots;
        animator.SetBool("Shooting", false);
    }
    else
    {
        timeBtwShots -= Time.deltaTime;
        if (timeBtwShots <= 0.3)
        {
            shot.volume = 1;
            shot.Play();
        }
        if (timeBtwShots <= 0.6)
        {
            animator.SetBool("Shooting", true);
        }
    }

    if (player.transform.position.y >= transform.position.y + 5.5f)
    {
        Destroy(gameObject);
    }
}
}

先感谢您!!

自我雅各布

最佳答案

您应该尝试使用Start函数:shot.gameObject.SetActive(true);
可能是问题所在。

关于c# - 无法播放禁用的音频源(Unity 2d),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53840417/

相关文章:

c# - 如何在 PowerShell C# cmdlet 中使用 WriteProgressCommand?

audio - 我似乎无法让音频在我的应用中正常工作?我正在使用AVAudio框架

windows - Windows 核心音频 API 中的 ERoles 枚举值代表什么?它们是相互排斥的吗?

c# - 数据集到 xml 空值

javascript - 带键线的 Neo4jClient

c# - File.ReadAllText 中的无效字符

ios - 停止 AUGraph 的卡顿

ubuntu - 如何在 ubuntu 15.10 上安装 unity3d

unity3d - OpenAI Unity - POST 请求无法正常工作(400 状态)

image - 如何将类型 'UnityEngine.Texture2D' 转换为 'UnityEngine.Sprite?