Regarding Scripting Optimisation

Hey GameDev Community!

In lecture #73 “Instantiate GameObject” of Unity2D course I see Rick do two things that I have questions about:

  1. Rick put gamestatus = FindObjectOfType<GameSession>(); into a method which is called when the Block is destroyed and also called the method AddScore() within. But I chose to put this line at the Start function. I just figured the Block objects should identify GameSession and then call the AddScore() only when needed, which is when it is destroyed and GameSession has already been found in Start.
    I want to understand what is the most correct way of executing this and why.

  2. In the DestroyBlock method (for me it is DestroyBrick) Rick called several other methods but he places them after Destroy(gameObject);. I chose to place the Destroy function last, since in my logic everything else has to be executed before the object is deleted.
    My question is, does it matter? and if it is how much?
    I try to optimise my scripting along as well to keep it clean and clear, for a good habbit.

public class Brick : MonoBehaviour
{
    [SerializeField] AudioClip destroyedClip;
    [SerializeField] GameObject brickSparklesVFX;

    // Cached reference
    Level level;
    GameSession gamestatus;
    private void Start()
    {
        // Assigning a level instance to find an object of type Level
        level = FindObjectOfType<Level>();
        level.BreakableBricksCount();

        gamestatus = FindObjectOfType<GameSession>();
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        DestroyBrick();
    }

    private void DestroyBrick()
    {
        level.BrickDestroyed();
        gamestatus.AddToScore();
        AudioSource.PlayClipAtPoint(destroyedClip, Camera.main.transform.position);
        TriggerSparklesVFX();
        Destroy(gameObject);
    }

    private void TriggerSparklesVFX()
    {
        GameObject sparkles = Instantiate(brickSparklesVFX, transform.position, transform.rotation);
        Destroy(sparkles, 1f);
    }
}

Hi @MunchingRaccoon,

Welcome to our community! :slight_smile:

  1. Generally, “correct” does not have a superlative. Something is either correct, or it’s not. You probably meant to ask which solution was more performant. To answer this question yourself, you’ll have to check how often the instance accesses the GameSession object during runtime. If it’s only once, it does not matter where you call FindObjectOfType<GameSession>(). If the object gets accessed multiple times during runtime, your solution is more performant than Rick’s because you cache the reference once and reuse it. Reusing something is definitely more performant than calling the expensive FindObjectOfType method multiple times.

  2. Destroy() does not destroy immediately, so it does not matter where you put the method call. However, in my personal opinion, it is “better” to put it at the end because, as you stated, that would follow our human logic. Generally, it is always a good idea to write graspable code. Since this course caters to beginners, Rick focuses rather on getting things done than on refactoring because most students are mainly interested in the former. Nevertheless, Rick encourages you to refactor your code yourself, to revise it and to explore different approaches/solutions because he knows that’s the best way to learn.

Did this answer your questions? :slight_smile:


See also:

Yes it did, thanks for clarifying Nina :slight_smile:

Getting things done is essential for the onboarding of this course, but sometimes while doing them I get curious and I want to go a little extra, so here I am.
And I love Rick, he is engaging and amusing, and his Aussie accent is a pleasure to my ears :smile:
You guys do a great job :+1:t3:

In my opinion, being curious and thinking for yourself is crucial for becoming better. Don’t hesitate to ask if you have any questions. :slight_smile:

1 Like

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

Privacy & Terms