[C#] Assets/BreakObject.cs(20,62): error CS0165: Use of unassigned local variable `broken'

Hello, today I ran into a problem while messing around with some concepts and I can’t seem to work out how to fix it. On one of my scripts unity straight up refuses to recognize a public variable as such, I’ve tried restarting several times and every time I get the same error. Here is a bit of the script in question.

public class BreakObject : MonoBehaviour {

	public GameObject broken;

	void Update () {

As that clearly shows ‘broken’ is a public variable yet no matter what I try unity refuses to recognize that it is and insists on treating it as a local variable.

Anyone had this problem or know how to fix it? It’s really annoying that I can’t go on with my project.

Thanks for your help!

You don’t show us enough of the code to figure out the issue. Any chance you are declaring this variable again inside a function? For example if you did this:

public class BreakObject : MonoBehaviour {
 
    public GameObject broken;
 
    void Update () {
        GameObject broken;

        broken.transform.positon = Vector3.zero;
     }
 }

…your code would generate the error.