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);
}