0
votes

I declared a private int tree_count in a class SpawnManager. void Start() and void Update() uses the variable as expected, but another method, public void Tree_destroyed seems to be using a different tree_count.

Here is my code.

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


public class SpawnManager : MonoBehaviour
{

    private int tree_count;

    // Start is called before the first frame update
    void Start()
    {
        tree_count = 500;
    }

    // Update is called once per frame
    void Update()
    {
        if (Time.time < 3.05)
        {
            print(tree_count);
        }

    }
    
    public void Tree_destroyed()
    {
        tree_count--;
        print(tree_count);
    }
}

void Update() prints 500 for the tree_count, but public void Tree_destroyed() prints 0, and goes to -1, -2,... for every method call.

public void Tree_destroyed() is called by an object with this script:

using System.Collections.Generic;
using UnityEngine;

public class TreeBehaviour : MonoBehaviour
{
    public GameObject spawnManager;

    // Start is called before the first frame update
    void Start()
    {
        Destroy(gameObject, 3);
    }

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

    private void OnDestroy()
    {
        spawnManager.GetComponent<SpawnManager>().Tree_destroyed();
    }
}

Any idea why it behaves like that? Any help would be appreciated.

EDIT 1: For the order of things that happen: first, private int tree_count is initialized, void Start() is called which sets tree_count to 500, void Update() is called every frame, which shows tree_count as 500, void Tree_destroyed is called after 3 seconds, which shows a different tree_count, void Update() is called in the next frame, showing tree_count as still 500.

EDIT 2: There are only 3 objects in the scene, the camera (no scripts attached), object with SpawnManager, and another object that calls Tree_destroyed when destroyed. I'm sure there are no duplicate objects or scripts.

1
you have two different instances of SpawnManager, and for the second you never call the Start method. That's my guess.Mario Vernari
Can you show the code where you actually call 'Tree_destroyed()' ?dimitar.d
Does everything happen in one and the same thread?dxiv
I'm not sure of what you mean by the same thread. If you're referring to how my computer processor processes these scripts, I have no idea.eClySe
It shouldn't matter in this case, since void onDestroy() is the one that calls Tree_destroyed() which only happens after 3 seconds. My output basically becomes 500, 500, 500, ..., 500, 500, 0, -1, -2,..., -5, -6, -7, 500, 500, 500 (the number of outputs near 0 is based on the number of trees I instantiated, in this example is 8).eClySe

1 Answers

0
votes

It's possible that because this is a class object that there are two instances of objects with the same private variable. Without seeing the rest of your project this is the most likely answer.