skip to Main Content

EDIT: I found the problem. I am simply a big dumb!

I removed "public static" from "IEnumerator" and that fixed everything.

I am a COMPLETE beginner to programming, and this is the first program I have ever written. To learn, I decided to remake simple games and Flappy Bird was my first attempt.

my problem: I declared a gameobject with "[SerializeField] GameObject" but cannot assign the prefab in the inspector.

This script "SpawnPipes" is attached to an empty gameobject "PipeSpawner"

    [SerializeField] public GameObject Pipe;

    //declared some other variables and stuff.. 

   
    void Start()
    {
        StartCoroutine(SpawnRoutine());
    }


    public static IEnumerator SpawnRoutine()
    {
        while (spawningOn == true)
        {
            Instantiate(Pipe,
                new Vector3(xpos, UnityEngine.Random.Range(minYPos, maxYPos), 0), Quaternion.identity);

            Debug.Log("spawned a pipe");

            yield return new WaitForSeconds(spawnRate);
        }
    } 

As you can see, I have declared a gameobject "Pipe" which is serialized, and so should show up in the inspector. And it did… at first.

I was doing some final bug fixing, when suddenly this code broke. I was editing a different script (which includes code that turns "spawningOn" to true or false depending on the gamestate) and when I tested it, a new error showed up my SpawnPipes script, which I hadn’t even touched!

Here is the error message:

An object reference is required for the non-static field, method, or property ‘SpawnPipes.Pipe’

The error refers me to the line

Instantiate (Pipe,

When I hover over "Pipe," is highlights my "GameObject Pipe;" so I know it’s properly relating the two. But it seems to think I do not have a prefab assigned, which I did.

The first thing I tried was changing my "GameObject Pipe" to "static GameObject Pipe". This removed the error, but when I tested the game the pipes did not spawn and I got the message that the object I was trying to instantiate was null, as in there was no object assigned to "GameObject Pipe" ..

I opened my gameobject "PipeSpawner" in the inspector, and looked at the "SpawnPipes" script, and for some reason, my Pipe field was gone. Remember, I declared my GameObject Pipe as both serialized AND public, so there is no reason it should be missing!

I erased the GameObject declaration code and rewrote it with and without "static", the field did not return. I reimported all of my prefabs, the field did not return. I relaunched Visual Studios and Unity, the field did not return.

Eventually, I do not remember what did it, but I did get the field to come back and I assigned my pipe prefab to it once again.

However, it STILL says

An object reference is required for the non-static field, method, or property ‘SpawnPipes.Pipe’

The prefab is assigned!! Only one field is named "Pipe" so I know it is assigned to the right field!! Please help!

2

Answers


  1. [SerializeField] public GameObject Pipe;
    

    is non-static field that means that static functions can’t access it, consider to turn the function to non-static

    public IEnumerator SpawnRoutine()

    in this case you won’t be able to call the function directly you need an instance of the class, you could use singleton to call the function.

    Login or Signup to reply.
  2. It sounds like you needed to add the script component to the gameObject and then, in the inspector with the gameObject selected, scroll down to the script and drag the gameObject into the empty bar where it goes. I could be wrong, because I’m not looking at your interface on Unity, but it sounds like maybe you need to do that.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search