Reset Level - calling a public method & using FindObjectOfType

Can someone tell me why the #1 syntax below doesn’t work in my “DestroyBlock” script — and why #2 syntax does? Isn’t the goal of both #1 & #2 syntax to call the CountBreakableBlocks (within my Level class/script) method upon the start of the scene so we have a number that tracks the blocks on the scene?

Also – how is it that

FindObjectOfType().AddToCurrentScore();

within the OnCollisionEnter2d(Collision2D collision) function, which is within my DestroyBlock script, works and it calls the AddToCurrentScore() within the class/script GameSession?

#1(example script)
private void Start()
{
level = FindObjectOfType().CountBreakableBlocks();
}

#2(example script)
private void Start()
{
level = FindObjectOfType();
level.CountBreakableBlocks();
}

Full DestroyBlock script (the script that works and is the one i’m using)
public class DestroyBlock : MonoBehaviour
{
public AudioClip clip;
Level level;

private void Start()
{
    level = FindObjectOfType<Level>();
    level.CountBreakableBlocks();
}

void OnCollisionEnter2D(Collision2D collision)
{
    AudioSource.PlayClipAtPoint(clip, Camera.main.transform.position);
    Destroy(gameObject);
    level.MinusBlocks();
    FindObjectOfType<GameSession>().AddToCurrentScore();
}

}

the script i’m using to add the # of blocks up at the beginning of each scene
public void CountBreakableBlocks()
{
startingBlocks++;
}

public class Level : MonoBehaviour
{

GameSession gameStatus;
[SerializeField] int startingBlocks;  //made public for de-bugging purposes only - just to check the script to show its tracking the correct # of blocks//
LoadNextScene loadNextScene;




public void CountBreakableBlocks()
{
    startingBlocks++;
}


public void MinusBlocks()
{
    startingBlocks--;
    if (startingBlocks <= 0)
    {

        loadNextScene = FindObjectOfType<LoadNextScene>();
        loadNextScene.GetNextScene();
        
    }
}

}

public class GameSession : MonoBehaviour
{

[Range(0.1f, 1f)] [SerializeField] float gameSpeed = 1.0f;
[SerializeField] int currentScore = 0;
[SerializeField] int scorePerBlockDestroyed = 100;
public TextMeshProUGUI textComponent;
int currentSceneIndex;



private void Awake()
{
    int gameStatusCount = FindObjectsOfType<GameSession>().Length;
    if (gameStatusCount >1)
    {
        gameObject.SetActive(false);
        Destroy(gameObject);
    }

    else
    {
        DontDestroyOnLoad(gameObject);
    }

}

private void Start()
{
    Time.timeScale = 1.0f;
    currentScore = 0;
    textComponent.text = currentScore.ToString();

}

void Update()
{
    SlowTime();
}

private void SlowTime()
{
    if (Input.GetMouseButton(1))
    {
        Time.timeScale = gameSpeed / 2;
    }

    else
    {
        Time.timeScale = gameSpeed;
    }
}

public void AddToCurrentScore()
{
        currentScore += scorePerBlockDestroyed;
        currentScore = currentScore;
        textComponent.text = currentScore.ToString();

}

public void ResetGameStatus()
{
    Destroy(gameObject);
}

UPDATE

I changed my script to reflect the following changes below — why did Rick have us use the following syntax:

public class DestroyBlock : MonoBehaviour
{


    public AudioClip clip;
    Level level;


    private void Start()
    {

        level = FindObjectOfType<Level>();
        level.CountBreakableBlocks();
    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        AudioSource.PlayClipAtPoint(clip, Camera.main.transform.position);
        Destroy(gameObject);
        level.MinusBlocks();
        FindObjectOfType<GameSession>().AddToCurrentScore();
    }

}

when we could have used the syntax below – which just directly finds the ObjectOfType - and calls the function we want?

public class DestroyBlock : MonoBehaviour
{
    public AudioClip clip;

    private void Start()
    {
        FindObjectOfType<Level>().CountBreakableBlocks();

    }
    void OnCollisionEnter2D(Collision2D collision)
    {
        AudioSource.PlayClipAtPoint(clip, Camera.main.transform.position);
        Destroy(gameObject);
        FindObjectOfType<Level>().MinusBlocks();
        FindObjectOfType<GameSession>().AddToCurrentScore();
    }
}

Hi LundresTaldre,

Find methods such as Find and FindObjectOfType are one of the slowest methods in Unity. For this reason, if we already looked for a reference, we ideally want to keep the reference instead of calling FindObjectOfType multiple times.

Is this what you wanted to know?


See also:

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

Privacy & Terms