An object reference is required to access a non-static member

147,639

Solution 1

You should make your audioSounds and minTime members static:

public static List<AudioSource> audioSounds = new List<AudioSource>();
public static double minTime = 0.5;

But I would consider using singleton objects instead of static members instead:

public class SoundManager : MonoBehaviour
{

    public List<AudioSource> audioSounds = new List<AudioSource>();
    public double minTime = 0.5;

    public static SoundManager Instance { get; private set; }

    void Awake()
    {
        Instance = this;
    }

    public void playSound(AudioClip sourceSound, Vector3 objectPosition, int volume, float audioPitch, int dopplerLevel)
    {    
        bool playsound = false;
        foreach (AudioSource sound in audioSounds) // Loop through List with foreach
        {  
            if (sourceSound.name != sound.name && sound.time <= minTime)
            {
                playsound = true;
            }
        }

        if(playsound) {
            AudioSource.PlayClipAtPoint(sourceSound, objectPosition);
        }

    }
}

Update from September 2020:

Six years later, it is still one of my most upvoted answers on StackOverflow, so I feel obligated to add: singleton is a pattern that creates a lot of problems down the road, and personally, I consider it to be an anti-pattern. It can be accessed from anywhere, and using singletons for different game systems creates a spaghetti of invisible dependencies between different parts of your project.

If you're just learning to program, using singletons is OK for now. But please, consider reading about Dependency Injection, Inversion of Control and other architectural patterns. At least file it under "stuff I will learn later". This may sound as an overkill when you first learn about them, but a proper architecture can become a life-saver on middle and big projects.

Solution 2

I'm guessing you get the error on accessing audioSounds and minTime, right?

The problem is you can't access instance members from static methods. What this means is that, a static method is a method that exists only once and can be used by all other objects (if its access modifier permits it).

Instance members, on the other hand, are created for every instance of the object. So if you create ten instances, how would the runtime know out of all these instances, which audioSounds list it should access?

Like others said, make your audioSounds and minTime static, or you could make your method an instance method, if your design permits it.

Solution 3

playSound is a static method meaning it exists when the program is loaded. audioSounds and minTime are SoundManager instance variable, meaning they will exist within an instance of SoundManager. You have not created an instance of SoundManager so audioSounds doesn't exist (or it does but you do not have a reference to a SoundManager object to see that).

To solve your problem you can either make audioSounds static:

public static List<AudioSource> audioSounds = new List<AudioSource>();
public static double minTime = 0.5;

so they will be created and may be referenced in the same way that PlaySound will be. Alternatively you can create an instance of SoundManager from within your method:

SoundManager soundManager = new SoundManager();
foreach (AudioSource sound in soundManager.audioSounds) // Loop through List with foreach
{
    if (sourceSound.name != sound.name && sound.time <= soundManager.minTime)
    {
        playsound = true;
    }
}

Solution 4

playSound is a static method in your class, but you are referring to members like audioSounds or minTime which are not declared static so they would require a SoundManager sm = new SoundManager(); to operate as sm.audioSounds or sm.minTime respectively

Solution:

public static List<AudioSource> audioSounds = new List<AudioSource>();
public static double minTime = 0.5;

Solution 5

Make your audioSounds and minTime variables as static variables, as you are using them in a static method (playSound).

Marking a method as static prevents the usage of non-static (instance) members in that method.

To understand more , please read this SO QA:

Static keyword in c#

Share:
147,639

Related videos on Youtube

James Eaton
Author by

James Eaton

Updated on September 15, 2020

Comments

  • James Eaton
    James Eaton over 3 years

    I'm having this error come up and I'm not sure why... I've tried to look it up, people are saying to create an object of the class or create the methods as static... but I'm unsure how.

    Here's my code below:

    public class SoundManager : MonoBehaviour {
    
    public List<AudioSource> audioSounds = new List<AudioSource>();
    public double minTime = 0.5;
    
    public static void playSound(AudioClip sourceSound, Vector3 objectPosition, int volume, float audioPitch, int dopplerLevel)
    {
        bool playsound = false;
        foreach (AudioSource sound in audioSounds) // Loop through List with foreach
        {
            if (sourceSound.name != sound.name && sound.time <= minTime)
            {
                playsound = true;
            }
        }
    
        if(playsound) {
            AudioSource.PlayClipAtPoint(sourceSound, objectPosition);
        }
    }
    }
    
    • Max Yankov
      Max Yankov about 10 years
      What line are you getting the error at?
    • devnull69
      devnull69 about 10 years
      playSound is a static method in your class, but you are referring to members like audioSounds or minTime which are not declared static so they will require a SoundManager sm = new SoundManager(); to operate as sm.audioSounds
    • Tony Hopkinson
      Tony Hopkinson about 10 years
      You need to make audiosounds static, or playsound not static.
    • チーズパン
      チーズパン about 10 years
      Or pass it as parameter when you are calling the method.
  • Cody Gray
    Cody Gray about 10 years
    Don't forget about minTime.
  • Max Yankov
    Max Yankov about 10 years
    My bad, added it to the answer.
  • devnull69
    devnull69 about 10 years
    don't forget minTime
  • Franck
    Franck about 10 years
    It would be nice to mention that it is not mandatory to be static fields. He can instead make the class static which also solve the problem.
  • Aage
    Aage about 10 years
    @devnull69 Thank you. Updated my answer.
  • devnull69
    devnull69 about 10 years
    Nope, static classes can only derive from object. The example class needs to derive from another class called MonoBehaviour
  • devnull69
    devnull69 about 10 years
    don't forget minTime ... as mentioned several times in the other answers
  • devnull69
    devnull69 about 10 years
    static class is not possible because it violates polymorphism ... a static class cannot derive from a base class (other than object)
  • RossBille
    RossBille about 10 years
    @devnull69 You're right i totally missed those, its a bit late for me. Answer has been updated
  • Franck
    Franck about 10 years
    Right i haven't noticed it
  • devnull69
    devnull69 about 10 years
    Hm is it just me or are you missing the constructor call new SoundManager()? Even a singleton needs a (single) call to the constructor
  • Max Yankov
    Max Yankov about 10 years
    It's Unity3d specifics: MonoBehaviors shouldn't be created with new operator; I'd rather leave an instance of SoundManager visible in the initial scene.