An Object Reference Is Required to Access Non-Static Member

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

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.

An object reference is required to access non-static member

It depends what you're trying to do. You can either make things static, or you can create an instance:

MainClass instance = new MainClass();
btn.Clicked += instance.StartClick;
btn_stop.Clicked += instance.StopClick;

I assume this isn't your real application, so it's hard to say what you should do in your real code. Personally I'd lean towards creating an instance - static variables represent global state which is usually a bad idea (in terms of testability etc). We can't tell whether that affects your situation or not though.

What's important is that you understand why you're getting the error message and why the above change fixes it. Once you understand that, you'll be in a better situation to make the best decisions.

Extension Method: 'an object reference is required to access non-static member' Error Thrown When Running in Travis CI

Extension method is just syntax sugar which allows user to call method defined in other classes from object directly. It's called like instance method, but really it's implemented as static method. I am not sure, but I think you are getting this error because Double.IsNegatie(double) really exists (see here), so method with same signature as your extension method already exists and runtime thinks ,that you are actually trying to call .Net framework static method instead of your extension method (note that you are not passing your optional parameter while calling, so exactly same signature) and throws error, because static method can't be called from instance. I think just renaming that method will work. Sorry if my theory is wrong

An object reference is required for a non-static field, method, or property

Your Main method is static and you can access only static members of the same class from a static method. All you need to do to make your EasyExploits.Module static as well:

private static readonly EasyExploits.Module module = new EasyExploits.Module();

An object reference is required to access non-static member `GroupMovement.rnd'

The problem is that the rnd variable is not static and you want to use it in static property, so make other fields and property not static or make the rnd static. Here how you can make rnd static.

public static Random rnd = new Random();

An Object reference is required for the non-static field, method, or property `Program.InsertLogData()`

you have to fix elastic client.

private static readonly IElasticClient _elasticClient = new ElasticClient();

// public Program()
// {
// }

After this you will have another error. You have to fix your foreach loop by placing
command.ExecuteNonQuery inside of the loop


connection.Open();
foreach (var item in response.Hits)
{
var id = item.Id;
var sourceItem = item.Source;
var json = _elasticClient.RequestResponseSerializer.SerializeToString(sourceItem);

command = new SqlCommand("INSERT INTO EsLogs (ELKLogID, LogMessage, DateCreated) VALUES ('" + id + "','" + json + "', getdate())", connection);

numrows = command.ExecuteNonQuery();
}

connection.Close();

and by the way you have to think about using parameters for your query

An object reference is required to access non-static member, Sigleton not working

This is not a singleton class. You should do something like this:

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

public class CanvasMgr : MonoBehaviour {
private CanvasMgr() { }
private static CanvasMgr instance = null;
public static CanvasMgr Instance
{
get
{
if (instance == null)
{
instance = new CanvasMgr();
}
return instance;
}
}

[SerializeField]
private GameObject defultUI;
[SerializeField]
private GameObject upgradeUI;

public GameObject UpgradeUI { get => upgradeUI; set => upgradeUI = value; }
public GameObject DefultUI { get => defultUI; set => defultUI = value; }

public void Refresh () {
if (ValueHolder.upgrademenuopen){
DefultUI.SetActive (false);
UpgradeUI.SetActive (true);
}
if(!ValueHolder.upgrademenuopen){
DefultUI.SetActive (true);
UpgradeUI.SetActive (false);
}
}
}`

Now you can call CanvasMgr.Instance.Refresh().



Related Topics



Leave a reply



Submit