I am making so simple mobile game with Unity. I am trying to change the positions of my bricks(named tag: tugla). My purpose is to my bricks when hit by the ball will be updated new position. While my ball touched the floor. There is no problem when no bricks hit. But, When I hit the second or some other bricks. My bricks are not going to position. Also, When I touch to brick with my ball. The brick will be destroyed. Such as; There are 4 bricks in my game. I hit the second one. But only the third and fourth bricks get a new position. How can I fix it?
My brick’s code:
public class tugla : MonoBehaviour
{
public static int toplamTuglaSayisi;
public Sprite[] tuglaSprite;
private int maxCarpmaSayisi;
private int carpmaSayisi;
GameObject kirmizitop;
// Start is called before the first frame update
void Start()
{
maxCarpmaSayisi = tuglaSprite.Length + 1;
toplamTuglaSayisi++;
kirmizitop = GameObject.FindGameObjectWithTag("kirmizitop");
}
// Update is called once per frame
void Update()
{
// -1.330 -1.868
}
private void OnTriggerEnter2D(Collider2D collision)
{
// İçinden kırmızı top geçince objeyi yok eder.
Debug.Log("kirmizi top geçti");
carpmaSayisi++;
if (carpmaSayisi >= maxCarpmaSayisi)
{
toplamTuglaSayisi--;
// Tugla bittiğini gösterir.
if (toplamTuglaSayisi <= 0)
{
Debug.Log("works");
// Oyun Yoneticisinin içerisindeki yazdığımız fonksiyonu çağırıyoruz.Bir sonraki sahneye geçiyoruz.
GameObject.FindObjectOfType<OyunKontrol>().GetComponent<OyunKontrol>().birsonrakisahne();
}
// çarptıgında destroy ediyor.
Destroy(gameObject);
}else
{
GetComponent<SpriteRenderer>().sprite = tuglaSprite[carpmaSayisi - 1];
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.name.Equals("top"))
{
carpmaSayisi++;
if (carpmaSayisi >= maxCarpmaSayisi)
{
toplamTuglaSayisi--;
// Tugla bittiğini gösterir.
if(toplamTuglaSayisi <= 0)
{
Debug.Log("works");
// Oyun Yoneticisinin içerisindeki yazdığımız fonksiyonu çağırıyoruz.Bir sonraki sahneye geçiyoruz.
GameObject.FindObjectOfType<OyunKontrol>().GetComponent<OyunKontrol>().birsonrakisahne();
}
// çarptıgında destroy ediyor.
Destroy(gameObject);
}else
{
GetComponent<SpriteRenderer>().sprite = tuglaSprite[carpmaSayisi - 1];
}
}
else
{
}
}
My ball’s code:
public class top : MonoBehaviour
{
GameObject topataci;
Rigidbody2D fizik;
GameObject acigostergesi;
GameObject[] tugla;
public GameObject top_arka;
// Start is called before the first frame update
void Start()
{
fizik = GetComponent<Rigidbody2D>();
topataci = GameObject.FindGameObjectWithTag("topataci");
acigostergesi = GameObject.FindGameObjectWithTag("acigostergesi");
tugla = GameObject.FindGameObjectsWithTag("tugla");
}
// Update is called once per frame
void Update()
{
//if (Input.GetMouseButton(0))
//{
// // Topa kuvvet uygulamaya yarar.
//// GetComponent<Rigidbody2D>().velocity = new Vector2(10f, 20f);
//}
//if (Input.GetMouseButtonUp(0))
//{
// for (int i = 0; i < 3; i++)
// {
// // Instantiate(top_arka, transform.position, transform.rotation);
// // yield return new WaitForSeconds(.1f);
// // SpawnIfNull();
// // top_arka.GetComponent<Rigidbody2D>().velocity = new Vector2(20f, 20f);
// }
//}
}
void OnCollisionEnter2D(Collision2D Collider)
{
if (Collider.gameObject.name.Equals("zemin"))
{
// Debug.Log("Hit!!!");
// fizik.MovePosition(fizik.position);
// Topun kuvvetini sıfırlamaya yarar.
GetComponent<Rigidbody2D>().velocity = new Vector2(0f, 0f);
// Zemine gelince pedallin yerini sıfırla.
topataci.GetComponent<Transform>().transform.position = new Vector3(0, -6.04f, 0);
// acigöstergesininin yerini ayarla
acigostergesi.GetComponent<Transform>().transform.position = new Vector3(transform.position.x, transform.position.y, 10f);
// Zemine gelince acigostergesinin açısını sıfırladım.
acigostergesi.GetComponent<Rigidbody2D>().rotation = 0;
// tuglalara yeni position ekle.
if (tugla.Length <= 0)
{
// SceneManager.LoadScene(1);
}
else
{
for (int i = 0; i < tugla.Length; i++)
{
Debug.Log("Getting new position");
tugla[i].GetComponent<Transform>().transform.position += Vector3.down * 0.538f;
}
}
// tugla.GetComponent<Transform>().transform.position += Vector3.down * 0.538f;
//new Vector3(tugla.transform.position.x , tugla.transform.position * 0.538f , )
}
}
>Solution :
From what I understand this should solve your problem, just reset the list of bricks before moving them.
if (tugla.Length <= 0)
{
// SceneManager.LoadScene(1);
}
else
{
tugla = GameObject.FindGameObjectsWithTag("tugla")
for (int i = 0; i < tugla.Length; i++)
{
Debug.Log("Getting new position");
tugla[i].GetComponent<Transform>.
().transform.position += Vector3.down * 0.538f;
}
}
You can also add the brick’s Start function to the following line
this.Tag = "tugla";