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!