Newbie Error Message Confusion

Unity is having problems compiling a script, and giving me errors:

Assets\UI\MoreSoilButton.cs(7,18): error CS1519: Invalid token ‘=’ in class, record, struct, or interface member declaration
Assets\UI\MoreSoilButton.cs(7,52): error CS1519: Invalid token ‘(’ in class, record, struct, or interface member declaration
Assets\UI\MoreSoilButton.cs(7,53): error CS1031: Type expected
Assets\UI\MoreSoilButton.cs(7,53): error CS8124: Tuple must contain at least two elements.
Assets\UI\MoreSoilButton.cs(7,53): error CS1026: ) expected
Assets\UI\MoreSoilButton.cs(7,53): error CS1519: Invalid token ‘“UsableSoil”’ in class, record, struct, or interface member declaration
Assets\UI\MoreSoilButton.cs(9,14): error CS1519: Invalid token ‘(’ in class, record, struct, or interface member declaration
Assets\UI\MoreSoilButton.cs(9,27): error CS8124: Tuple must contain at least two elements.

The script is here:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoreSoilButton : MonoBehaviour
{
    soil_object_ = GameObject.FindGameObjectWithTag("UsableSoil");

    Debug.Log(soil_object_)
}

I’m creating a 2d farming idle game, and the script is for a button that will give the player more soil to farm with. I’m new to coding in general and trying to debug every step of the way to make sure the code is working how I think it should. I’m using the debug log here to make sure I’m getting the right game object. There is only one game object under the tag “UsableSoil.” Why am I getting these errors?

You have two lines of code inside of a class, but not in a method.

You need to define operations inside functions (along with declaring a type of value for soil_object_):

public class MoreSoilButton : MonoBehaviour
{
    public void GetSoil()
    {
        GameObject soil_object_ = GameObject.FindGameObjectWithTag("UsableSoil");
        Debug.Log(soil_object_)
    }
}

Best to touch up on some basic C#, as you will need it to be able to get anything done.

You’re right. I’ve been doing that, pouring over C# basics and the Unity API documentation for hours, and the latter isn’t beginner friendly.

I looked at tutorials for player avatar movement and they defined variables outside functions under a class so I thought you could do the same here, but I guess you can’t when your definition uses functions.

I found the answer eventually, thank you.

The times you would define variables outside functions under a class would be 1, when more than one function within the class uses it, and 2, when they need to be accessed by other classes or from the inspector.

When it comes to variables you define within a class, this should be of great help to you: is SerializeField the same as making a variable public?

It’s also worth noting that nothing has to be declared as private. Declaring it as anything other than public makes it automatically declared as private. Declaring things as private is just an aesthetic / organizational choice.