'Error: Assets/beandefine.cs(23,1) (sorry Dani) Type or namespace definition, or end of file expected

I get this error so much! It is not the filename though, because it is beandefine.cs (I want to define a GameObject)

Here is the code:

using System.Collections.Generic;
using UnityEngine;

public class beandefine : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void BEAN()
    {
            public var BeanPlayer = GameObject.BeanPlayer;
    }
}

PLEASE HELP. I think the code is good, but it is not working. Verified 3 times and yep, still good. So no idea what is happening, but it doesn't want to work. Thanks for help.



Solution 1:[1]

When will people stop making class level variables inside methods and wondering why the compiler says no? Apparently not today.

Problem is here

void BEAN() { public var BeanPlayer = GameObject.BeanPlayer; }

What do you actually want? If you want the variable at class level, then you need it outside the method. If you only need it in the method drop the public. And BeanPlayer will throw an error as GameObject has no knowledge of such people things. Did you mean to use getcomponent?

Solution 2:[2]

I needed to put public var BeanPlayer = GameObject.BeanPlayer; in

public class beandefine : MonoBehaviour {

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BugFinder
Solution 2