Set object from array to active only whilst a specific number is returned

Hi,

I was wondering if anyone would be able to help with this problem?

I have 11 trees which are set to active when their index number is returned from the calcSymbolIndex.

This works fine: however, I need it so that the tree with the corresponding index number is only active whilst that index number is currently being returned.

So whilst the calcSymbolIndex returns 0, the first tree in the array is set active. Once the calcSymbolIndex returns 1, the first tree in the array is set to inactive (and the second tree set to active).

I just can’t get my head around how I should write this, and was wondering if anyone could give me any pointers?

public class SlotWorld : MonoBehaviour {

	Slot slot;
	public GameObject[] trees = new GameObject[11];

	void Awake()
	{
		foreach (GameObject tree in trees)
		{
			tree.SetActive(false);
		}
	}

	void Start () 
	{
		slot = GetComponent<Slot>();
	}

	void Update()
	{
		trees[calcSymbolIndex(0,0)].SetActive(true);
	}

	int calcSymbolIndex(int reel, int range)
	{
		int symbolIndex = slot.reels[reel].symbols[range].GetComponent<SlotSymbol>().symbolIndex;
		Debug.Log (symbolIndex);
		return symbolIndex;
	}

}

Best,
Laurien

just save the previous index and when a new one comes in that’s different, disable the tree from that index, save the new one and so on

@laurienvictoria

Here’s a bit of code with one option to describe what hexagonius is suggesting

public class SlotWorld : monoBehaviour {

    // other variable declarations

    private int lastIndex = -1;
    private int thisIndex = -1;

    // other function declarations and stuff

    void Update()
    {
        thisIndex = calcSymbolIndex(0,0);
        if (thisIndex != lastIndex)
        {
            // really only need to do this when the indexes are different
            trees[lastIndex].SetActive(false);
            trees[thisIndex].SetActive(true);
            lastIndex = thisIndex;
        }
    }

    // and the rest of your code
}

Hope this helps