Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

So I'm getting a CS1022 error and Idk how to fix it. Any Answers?

Discussion in '2D' started by MadaxIsBored, Feb 19, 2021.

  1. MadaxIsBored

    MadaxIsBored

    Joined:
    Feb 19, 2021
    Posts:
    1
    Code (CSharp):
    1. using UnityEngine.Audio;
    2. using System;
    3. using UnityEngine;
    4. using System.Diagnostics;
    5. public class AudioManager : MonoBehaviour
    6. {
    7.  
    8.     public Sound[] sounds;
    9.  
    10.     public static AudioManager instance;
    11.  
    12.     void Awake()
    13.     {
    14.         if (instance == null)
    15.             instance = this;
    16.         else
    17.         {
    18.             Destroy(gameObject);
    19.             return;
    20.         }
    21.  
    22.         DontDestroyOnLoad(gameObject);
    23.  
    24.         foreach (Sound s in sounds)
    25.         {
    26.             s.source = gameObject.AddComponent<AudioSource>();
    27.             s.source.clip = s.clip;
    28.  
    29.             s.source.volume = s.volume;
    30.             s.source.pitch = s.pitch;
    31.             s.source.loop = s.loop;
    32.         }
    33.     }
    34. }
    35.  
    36.  
    37. void Start() {
    38.  
    39.     Play("Theme");
    40. }
    41.  
    42.     public void Play (string name)
    43.     {
    44.         Sound s = Array.Find(sounds, sound => sound.name == name);
    45.      if (s == null)
    46.     {
    47.         Debug.LogWarning("Sound: " + name + " not found");
    48.         return;
    49.     }
    50.     s.source.Play();
    51.     }
    52. }
    53.  
     
  2. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    921
    Hello @MadaxIsBored
    Could you paste the whole error you are seeing? It is needed in order to understand what goes wrong.
     
  3. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    497
    Get rid of the } on line 34. That one is closing your class and making your Start and Play methods be outside the class.

    But as already asked, next time you want help you need to post the entire error message and not just the code as without that we will be guessing at what the error is.
     
    Ted_Wikman likes this.