Mega challenge short solution - need confirmation

For this challenge insted of creating methods and stuff I’ve found the easiest working solution:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Level : MonoBehaviour
{
    [SerializeField] Text blockCountObj;
    int blockCount;
    // Start is called before the first frame update
    void Start()
    {
    }

    void BlockCountFunction ()
    {
        blockCountObj.text = blockCount.ToString();
    }
    // Update is called once per frame
    void Update()
    {
        blockCount = GameObject.FindGameObjectsWithTag("Block").Length;
        Debug.Log(blockCount);
        BlockCountFunction();
        NexLevel();
    }

    void NexLevel()
    {
        if (blockCount == 0)
        {
            SceneManager.LoadScene(0);
        }
    }
}

Is this a legal solution or not? It works as intended, blocks are being counted, the number is correctly updated.

@ben @Rick_Davidson I summon you guys :slight_smile:

Seems like it would would be fine but computationally more intensive

1 Like

Hi Arthur,

Welcome to our forum. :slight_smile:

If your solution works, it’s valid by default. The only problem I see is the FindGameObjectsWithTag method being called in Update. Unity’s Find methods are very slow. They should not be called each frame. If they were not that slow, your solution would be perfect, and Rick would have done the same as you. Nevertheless, you were on the right track. Keep it up!

1 Like

Thanks a lot guys, that’s exactly what I wanted to hear. To get it working is one thing, but I should be thinking about what will it do in the long run.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms