강의

멘토링

커뮤니티

Cộng đồng Hỏi & Đáp của Inflearn

Hình ảnh hồ sơ của juijiji
juijiji

câu hỏi đã được viết

[Unity 3D] Đang hoạt động! Tạo game sinh tồn - nâng cao

Tổng triển khai

효과음 넣을 때 오류

Viết

·

439

0

효과음을 넣으면 그 전에는 잘 돼다가 안돼요ㅠㅠ

NullReferenceException: Object reference not set to an instance of an object

GunController.PlaySE (UnityEngine.AudioClip _clip) (at Assets/script/GunController.cs:51)

GunController.Shoot () (at Assets/script/GunController.cs:44)

GunController.Fire () (at Assets/script/GunController.cs:40)

GunController.TryFire () (at Assets/script/GunController.cs:34)

GunController.Update () (at Assets/script/GunController.cs:23)

이런 오류메세지가 떠요ㅠㅠ

unity

Câu trả lời 1

0

dydybest님의 프로필 이미지
dydybest
Người đặt câu hỏi

전체 코드:

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

public class GunController : MonoBehaviour
{
    [SerializeField]
    private Gun currentGun;

    private float currentFireRate;

    private AudioSource audioSource;

    void start()
    {
        audioSource = GetComponent<AudioSource>();
    }

// Update is called once per frame
    void Update()
    {
        GunFireRateCalc();
        TryFire();
    }
    private void GunFireRateCalc()
    {
        if(currentFireRate > 0)
            currentFireRate -= Time.deltaTime;
    }
    private void TryFire()
    {
        if(Input.GetButton("Fire1") && currentFireRate <= 0)
        {
            Fire();
        }
    }
    private void Fire()
    {
        currentFireRate = currentGun.fireRate;
        Shoot();
    }
    private void Shoot()
    {
        PlaySE(currentGun.fire_Sound);
        currentGun.muzzleFlash.Play();
        Debug.Log("총알 발사함");
    }

    private void PlaySE(AudioClip _clip)
    {
        audioSource.clip = _clip;
        audioSource.Play();
    }
}
Hình ảnh hồ sơ của juijiji
juijiji

câu hỏi đã được viết

Đặt câu hỏi