Static - Non static ... what the..?

I’m getting totally confused re the use of static and non static and no matter how I change my code to include or not include the keyword I get errors saying I need it or I don’t need it?

In script A which is attached to many GameObjects there is a line of code that does this:
DisplayPosterContent.DidFindTarget ();

In DisplayPosterContent I have this:

public static void DidFindTarget() {

  // build the url to the selected project
  projectURL = "url path appears here" + projectName;


private IEnumerator GetProjectArray () {
  WWW www = new WWW (projectURL);
  // wait for response
  yield return www;

  .... rest of code here		

So for DidFindTarget if I don’t prefix with Static I get the error An object reference is required to access non-static member

When that is fixed I get the same message for the call to GetProjectArray() so I add static to the GetProjectArray method and I’m back to the same error this time on IEnumerator?

Totally confusticated :frowning:

static means the method or field is not associated with an instance of the script. This means you don’t need to get a reference using GetComponent or alternatives before calling the method.

Non-static fields and methods need an instance to be called, i.e. theObject.NonStaticMethod(). Notice this starts with the object reference and not the name of the class. When calling the non-static method GetProjectArray you need to first get a reference to an instance. GetComponent and its alternatives will help you there.

You could also make DidFindTarget non-static and get a reference to call it.

Another option is to make GetProjectArray static. Keep in mind the same static vs. non-static restrictions apply here as well.

Official static documentation static modifier - C# Reference | Microsoft Learn

well in simple words:
static variable and methods (Static Members) belong to the class it self not the instances.
you don’t need to create an instance to use the Static Members of the class.
for example:

class classA{
        public int a;

In this class you have to Create instances to access the “int a” variable , but you can create multiple instances.

ClassA c1 = new ClassA();
ClassA c2 = new ClassA();
c1.a = 123; //These two instances are completely separate , They have the same structure , but they are different objects   
c2.a = 321;

You can’t do that with static classes
,Here is an example:

static class classB{
        public static int b;

You can’t create instances of this class, but you can use it anywhere without creating an instance first:

classB.b=321; // see the difference?  

Simple fix for your code?
both methods don’t have to be static … that’s what i know about Co-routines :wink:
and you don’t attach static classes to objects in Unity :stuck_out_tongue: