首页 > 解决方案 > 'public / static' 修饰符对此项无效

问题描述

我创建了一个脚本来在游戏中添加声音,比如跳跃和击打,我有一个我无法解决的错误仍然说The modifier 'public' is not valid for this item,我统一说,我的方式The static modifier is not valid for this item可以帮助我吗?

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

public class SoundManagerScript : MonoBehaviour
{
    public static AudioClip Hit_HurtSound, Jump2Sound;
    static AudioSource audioSrc;

    // Start is called before the first frame update
    void Start()
    {
        Hit_HurtSound = Resources.Load<AudiotClip> ("Hit_Hurt");
        Jump2Sound = Resources.Load<AudiotClip> ("Jump2");

        audioSrc = GetComponent<AudioSource> ();
    }

    // Update is called once per frame
    void Update()
    {
              public static void PlaySound (string clip)
        {
            switch (clip)
            {
                case "Jump2":
                    audioSrc.PlayOneShot(Jump2Sound);
                    break;

                case "Hit_Hurt":
                    audioSrc.PlayOneShot(Hit_HurtSound);
                    break;
            }
        }
    }
}

标签: c#

解决方案


就像@DavidG 在他的评论中所说,你不能有一个方法和一个方法。它应该如下所示:

public static void PlaySound (string clip)
{
    switch (clip)
    {
        case "Jump2":
            audioSrc.PlayOneShot(Jump2Sound);
            break;

        case "Hit_Hurt":
            audioSrc.PlayOneShot(Hit_HurtSound);
            break;
    }
}

您必须删除此行(及其}

void Update()
{

推荐阅读