SetActive issues on infinite runner.

Hi,

I’m making an infinite runner as a first game that I’ll be doing without a step by step tutorial. It is based on the flash game Shift which you can find on Armor Games. The way I have decided to do the switching (where white turns to black and black turns to white) is by setting the relevant blocks to be inactive at the right time.

I have checked all over the forums and can’t find similar issues, so it may just be me. However, I’d really like help solving this problem.

The relevant code:

    void Update () {
        bool change = Input.GetButtonDown("Switch");

        if (change == true) {
            black = GameObject.FindGameObjectsWithTag("Black");
            white = GameObject.FindGameObjectsWithTag("White");
            blackBlocks = GameObject.FindGameObjectsWithTag("BlackBlocks");
            whiteBlocks = GameObject.FindGameObjectsWithTag("WhiteBlocks");

            foreach(GameObject gO in black) {
                gO.gameObject.tag = "White";
                gO.gameObject.GetComponent<Renderer>().material = whiteMaterial;
            }
            foreach(GameObject gO in white) {
                gO.gameObject.tag = "Black";
                gO.gameObject.GetComponent<Renderer>().material = blackMaterial;
            }
            foreach(GameObject gO in blackBlocks) {
                if(isBlackTrue == true){
                    gO.gameObject.SetActive(false);
                    Debug.Log("blacktrue");
                }
                else if (isBlackTrue == false) {
                    gO.gameObject.SetActive(true);
                }
            }
            foreach(GameObject gO in whiteBlocks) { 
                if(isWhiteTrue == true){
                    gO.SetActive(false);
                }
                else if (isWhiteTrue == false) {
                    gO.SetActive(true);
                }
            }
            isBlackTrue = !isBlackTrue;
            isWhiteTrue = !isWhiteTrue;
        }

What this code does, is gets the blocks with the relevant tags, and switches them around. The black and white are the background, and the blackBlocks and whiteBlocks are being set inactive as they are the ones the player needs to switch to try to land on.

If you look at the debug.log on line 21, you see it should likely activate every two times the player switches, but it only activates once, and never again, suggesting to me that something is wrong with the booleans.

Any help is appreciated and, if I cannot find a solution, alternative methods of achieving the same goal are also welcome.

(Sorry if this is meant to be in scripting!)

ADESONOM

I think your problem is that GameObject.FindGameObjectsWithTag only finds objects that are active.

Well, I didn’t know that! What should I use instead? Should I just move them backwards in the scene so they are behind the background and the player can’t land on them?

All find methods have the same “problem”. In your case, I would generate a list of objects once and keep reusing that - it’s also faster than searching through the hierarchy time and time again. Are you generating the objects dynamically? Then add them to a list when you instantiate them. Are they already in the scene, then have them all activated at the start and do a GameObject.FindGameObjectsWithTag in your Start() and reuse the result from that search later on.

And, BTW, using foreach in Update can cause problems with garbage collection. It’s better to use an ordinary for loop.

In the final version of the game, I’m going to use object pooling to enable chunks in front of the player, and disable them behind the player. I suppose I could do this, but on a single chunk level? So each chunk finds out what objects it has and enables and disables them.