cannot change boolean of parent object

Ok so I’m making a small 2d survival game and I made a little forest.

The forest is made out of empty objects which spawn tree prefabs at their locations every x seconds as their childs. The problem is that they will keep spawning new trees over and over again, so to fix this I made a bool that keeps track if there is a tree there or not. By making the bool false in the editor, the script will spawn a new tree. This is working as expected.

Now here’s my problem. The trees are being cut by a script which is placed on the tree prefabs, which only spawn when the game starts. (in the Start() function). I am trying to change the bool with the following command which should change the doesTreeExist boolean to false, but it doesn’t.

transform.parent.GetComponent<SpawnTrees>().doesTreeExist = false;

I do not understand why it doesn’t change the boolean. Is it because the tree is only instantiated when the game starts? If so, what would be a better way of making the trees?
I will also point out that the command is not executed at the start of the game, just when the player cuts a tree down.

Where are you trying to call this method from? This is accessing it by transform.parent, which means it is looking for the script on the parent gameObject of whatever this script is attached to. Make sure that your script is on the parent object and not some child object, otherwise, it is looking for the script in the wrong place.

It is best to set up your reference on Awake

private SpawnTrees m_SpawnTrees;

private void Awake()
{
     m_SpawnTrees = GetComponent<SpawnTrees>();
}

Then probably have a public method you can access and modify the boolean value

private bool m_DoesTreeExist;

public void DoesTreeExist(bool exist)
{
    m_IsTreeCut = exist;
}