CS0116 Error


Complete scripting beginner here, basically just copied and pasted this from elsewhere, but I am getting a bunch of CS0116 errors. I’m sure it’s super obvious what the problem is, but unfortunately I don’t have a clue! Thanks!

using UnityEngine.UI;

public float time_min = 1f;
public float time_max = 3f;
public Sprite sprites = new Sprite[0];
public Image image;

void Start() {

public IEnumerator ShowRandomImage() {
while(true) {
image.sprite = sprites[Random.Range(0, sprites .Length)];
image.enabled = true;
yield return new WaitForSeconds(1);
image.enabled = false;
yield return new WaitForSeconds(Random.Range(time_min, time_max));


Googling CS0116 will bring you to Microsoft’s documentation page for that error, which gives this explanation:

A namespace does not directly contain members such as fields or methods.
Inside a namespace, the compiler only accepts classes, structures, unions, enumerations, interfaces, and delegates. This error is often generated by developers from a C/C++ background who forget that in C#, methods and variables must be declared and defined within a struct or class. For more information, see General Structure of a C# Program.

In C#, you can’t have variables and functions hanging around outside of a class. They have to be inside a class/struct/etc to work (and the class has to inherit from MonoBehaviour if you want to use Start, Update, and coroutines). You can see the basic framework of a C# monobehaviour script here, which is also the basic template that unity and monodevelop will generate for you when you create a new C# script.