Changing a variable at a certain object size.

Hello. I would like to ask a question about changing variables. I’m still quite new to Unity and C#, so I’m sorry if the question sounds stupid. I wrote code for a simple game, and according to my idea, objects decrease every second if they do not collect the so-called “bonus”, and when their size is less than the specified value, they should be deleted, and one should be added to the value of “CD”. And when the CD value>= 3 (that is, three objects were deleted), the scene should switch to itself(that is, the level is reloaded). For some reason, a unit is simply not added to the “CD” value. I tried at the moment when the object becomes small and is deleted to change a variable of the bool type or create a white square on the stage and all this just didn’t work, and the game object was deleted anyway( That is, it did not give out an error)! I will send the full code of the project, if someone can check please, you are my last hope, because I did not find anything on Russian-language and English-language YouTube(I’m from Russia).

Important part of the code here or full code in Uploaded file:

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

public class Controller : MonoBehaviour
{

public Transform SpawnZone;

public float grow = 0.4f;
public float antigrow = 0.3f;
public float CD;

float MinX;
float MinY;

public float antigrowTime;
public float StartantigrowTime;

private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.tag == “bonus”)
{
transform.localScale += new Vector3(grow, grow, grow);

Destroy(collision.gameObject);
}
}

void Start()
{
rb = GetComponent();
}

void Update()
{
if (antigrowTime <= 0)
{
transform.localScale -= new Vector3(antigrow, antigrow, antigrow);
antigrowTime = StartantigrowTime;
}

else
{
antigrowTime -= Time.deltaTime;
}

MinX = SpawnZone.localScale.x;
MinY = SpawnZone.localScale.y;

if (MinX < 0.2 || MinY < 0.2)
{
Destroy(this.gameObject);
CD++;
}

if (CD >= 3)
SceneManager.LoadScene(“MainMenu”);
}

7501085–924164–Controller.cs (2.4 KB)

It may be useful to look up Youtube tutorials for the type of game you describe, since you seem to have a LOT of detail about it in your mind.

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

Also, rather than tacking on silly useless polls, please learn how to post code properly:

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

Use code tags: