Forum begins after the advertisement:


[Part 25] Optimising the SpawnManager reference in GameManager

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 25] Optimising the SpawnManager reference in GameManager

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #19155
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    In Part 25 of the series, we added a reaper to our game, and in the GameManager, we disable the spawn manager when time runs out on the stopwatch.

    A minor optimisation can be made on the UpdateStopwatch() function of GameManager — instead of using FindObjectOfType() to get a reference, we can use SpawnManager.instance, as the class automatically assigns a static reference of itself for convenience.

    void UpdateStopwatch()
    {
        stopwatchTime += Time.deltaTime * ClockSpeed;
        UpdateStopwatchDisplay();
    
        if (stopwatchTime >= timeLimit && !levelEnded)
        {
            levelEnded = true;
    
            // Set the enemy/event spawner GameObject inactive to stop enemies from spawning and kill the remaining enemies onscreen.
            FindObjectOfType<SpawnManager>().gameObject.SetActive(false);
            SpawnManager.instance.gameObject.SetActive(false);
            foreach (EnemyStats e in FindObjectsOfType<EnemyStats>())
                e.SendMessage("Kill");
    
            // Spawn the Reaper off-camera
            Vector2 reaperOffset = Random.insideUnitCircle * 50f;
            Vector2 spawnPosition = GetRandomPlayerLocation() + reaperOffset;
            Instantiate(reaperPrefab, spawnPosition, Quaternion.identity);
    
    
        }
    }
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: