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
Answer by hexagonius · Jan 12, 2017 at 09:45 PM
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
Thanks so much for your reply!
I'm being a bit dim - but how do I save the previous index, I've just tried storing it in an array and using an if statement to see if the next value is the same or not - but if the calcSymbolIndex is being called all the time in the update function, how do I save out the returned values?
Answer by Robert_s_a · Mar 16, 2017 at 02:25 AM
@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
Your answer
Follow this Question
Related Questions
gameObject.SetActive (false); Not working 0 Answers
OnTriggerEnter not working 2 Answers
SetActive is just working if i press twice in the first time 2 Answers
Clicking Cube And Canvas Appears and disappears 1 Answer
Basic Code Problem 1 Answer