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

时间:2018-12-18 20:17:23

标签: 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);
    }
}
}

提前谢谢!

自我|雅各布

1 个答案:

答案 0 :(得分:0)

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