Need help to make my script easier!

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

public class GameOverScript : MonoBehaviour
{
    public bool[] Destroyed = new bool[31];
    BoxCollider2D Collid1;
    BoxCollider2D Collid2;
    BoxCollider2D Collid3;
    BoxCollider2D Collid4;
    BoxCollider2D Collid5;
    BoxCollider2D Collid6;
    BoxCollider2D Collid7;
    BoxCollider2D Collid8;
    BoxCollider2D Collid9;
    BoxCollider2D Collid10;
    BoxCollider2D Collid11;
    BoxCollider2D Collid12;
    BoxCollider2D Collid13;
    BoxCollider2D Collid14;
    BoxCollider2D Collid15;
    BoxCollider2D Collid16;
    BoxCollider2D Collid17;
    BoxCollider2D Collid18;
    BoxCollider2D Collid19;
    BoxCollider2D Collid20;
    BoxCollider2D Collid21;
    BoxCollider2D Collid22;
    BoxCollider2D Collid23;
    BoxCollider2D Collid24;
    BoxCollider2D Collid25;
    BoxCollider2D Collid26;
    BoxCollider2D Collid27;
    BoxCollider2D Collid28;
    BoxCollider2D Collid29;
    BoxCollider2D Collid30;
    // Start is called before the first frame update
    void Start()
    {
        Collid1 = GameObject.Find("GameOver").GetComponent<BoxCollider2D>();
        Collid2 = GameObject.Find("GameOver (1)").GetComponent<BoxCollider2D>();
        Collid3 = GameObject.Find("GameOver (2)").GetComponent<BoxCollider2D>();
        Collid4 = GameObject.Find("GameOver (3)").GetComponent<BoxCollider2D>();
        Collid5 = GameObject.Find("GameOver (4)").GetComponent<BoxCollider2D>();
        Collid6 = GameObject.Find("GameOver (5)").GetComponent<BoxCollider2D>();
        Collid7 = GameObject.Find("GameOver (6)").GetComponent<BoxCollider2D>();
        Collid8 = GameObject.Find("GameOver (7)").GetComponent<BoxCollider2D>();
        Collid9 = GameObject.Find("GameOver (8)").GetComponent<BoxCollider2D>();
        Collid10 = GameObject.Find("GameOver (9)").GetComponent<BoxCollider2D>();
        Collid11 = GameObject.Find("GameOver (10)").GetComponent<BoxCollider2D>();
        Collid12 = GameObject.Find("GameOver (11)").GetComponent<BoxCollider2D>();
        Collid13 = GameObject.Find("GameOver (12)").GetComponent<BoxCollider2D>();
        Collid14 = GameObject.Find("GameOver (13)").GetComponent<BoxCollider2D>();
        Collid15 = GameObject.Find("GameOver (14)").GetComponent<BoxCollider2D>();
        Collid16 = GameObject.Find("GameOver (15)").GetComponent<BoxCollider2D>();
        Collid17 = GameObject.Find("GameOver (16)").GetComponent<BoxCollider2D>();
        Collid18 = GameObject.Find("GameOver (17)").GetComponent<BoxCollider2D>();
        Collid19 = GameObject.Find("GameOver (18)").GetComponent<BoxCollider2D>();
        Collid20 = GameObject.Find("GameOver (19)").GetComponent<BoxCollider2D>();
        Collid21 = GameObject.Find("GameOver (20)").GetComponent<BoxCollider2D>();
        Collid22 = GameObject.Find("GameOver (21)").GetComponent<BoxCollider2D>();
        Collid23 = GameObject.Find("GameOver (22)").GetComponent<BoxCollider2D>();
        Collid24 = GameObject.Find("GameOver (23)").GetComponent<BoxCollider2D>();
        Collid25 = GameObject.Find("GameOver (24)").GetComponent<BoxCollider2D>();
        Collid26 = GameObject.Find("GameOver (25)").GetComponent<BoxCollider2D>();
        Collid27 = GameObject.Find("GameOver (26)").GetComponent<BoxCollider2D>();
        Collid28 = GameObject.Find("GameOver (27)").GetComponent<BoxCollider2D>();
        Collid29 = GameObject.Find("GameOver (28)").GetComponent<BoxCollider2D>();
        Collid30 = GameObject.Find("GameOver (29)").GetComponent<BoxCollider2D>();
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            Destroy(collision.gameObject);
        }
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (Destroyed[1] == true)
        {
            Collid1.enabled = true;
        }
        if (Destroyed[2] == true)
        {
            Collid2.enabled = true;
        }
        if (Destroyed[3] == true)
        {
            Collid3.enabled = true;
        }
        if (Destroyed[4] == true)
        {
            Collid4.enabled = true;
        }
        if (Destroyed[5] == true)
        {
            Collid5.enabled = true;
        }
        if (Destroyed[6] == true)
        {
            Collid6.enabled = true;
        }
        if (Destroyed[7] == true)
        {
            Collid7.enabled = true;
        }
        if (Destroyed[8] == true)
        {
            Collid8.enabled = true;
        }
        if (Destroyed[9] == true)
        {
            Collid9.enabled = true;
        }
        if (Destroyed[10] == true)
        {
            Collid10.enabled = true;
        }
        if (Destroyed[11] == true)
        {
            Collid11.enabled = true;
        }
        if (Destroyed[12] == true)
        {
            Collid12.enabled = true;
        }
        if (Destroyed[13] == true)
        {
            Collid13.enabled = true;
        }
        if (Destroyed[14] == true)
        {
            Collid14.enabled = true;
        }
        if (Destroyed[15] == true)
        {
            Collid15.enabled = true;
        }
        if (Destroyed[16] == true)
        {
            Collid16.enabled = true;
        }
        if (Destroyed[17] == true)
        {
            Collid17.enabled = true;
        }
        if (Destroyed[18] == true)
        {
            Collid18.enabled = true;
        }
        if (Destroyed[19] == true)
        {
            Collid19.enabled = true;
        }
        if (Destroyed[20] == true)
        {
            Collid20.enabled = true;
        }
        if (Destroyed[21] == true)
        {
            Collid21.enabled = true;
        }
        if (Destroyed[22] == true)
        {
            Collid22.enabled = true;
        }
        if (Destroyed[23] == true)
        {
            Collid23.enabled = true;
        }
        if (Destroyed[24] == true)
        {
            Collid24.enabled = true;
        }
        if (Destroyed[25] == true)
        {
            Collid25.enabled = true;
        }
        if (Destroyed[26] == true)
        {
            Collid26.enabled = true;
        }
        if (Destroyed[27] == true)
        {
            Collid27.enabled = true;
        }
        if (Destroyed[28] == true)
        {
            Collid28.enabled = true;
        }
        if (Destroyed[29] == true)
        {
            Collid29.enabled = true;
        }
        if (Destroyed[30] == true)
        {
            Collid30.enabled = true;
        }
    }

}

Hi, I wrote this Script for a small Testgame I made, all it basically does is check if a certain GameObject is destroyed and then activates a BoxCollider2D

Is there any way I can simplify this?

(Script in attached File)

8494193–1130372–GameOverScript.cs (6.6 KB)

Nobody wants to download a random file from the internet. Just post the code into the thread using Code tags .

Whenever you find yourself in a situation where you think you had to manually label variables with numbers, there is something wrong. Usually that would mean you miss an array, but in this case, if i understand you correctly, the entire script seems unnecessary. Simply said, i understand it you have 30 obstacles which insta-kill your player and you want to trigger some game-over function when he touches them? Simply write a small script that has an OnCollisionEnter, in which it checks if the object it collided with is the player, and if so, calls the game-over function. Attach it to any such obstacle and you are done.

I might be misunderstanding what you want tho, as i have no idea why the player can collide with the GameOverScript itself, or why there is 30 colliders you appear to be enabling… to me your description of what you want does not align with the script you wrote at all. But no matter what you want in the end, there certainly is a way easier way to achieve it :slight_smile:

Edit: I agree with @RadRedPanda but still downloaded it lmao

I´m new to C# so I dont know that much about it tbh

So this is basically a “master controller” which is responsible for 30 different GameObjects and this is the only way that worked for me, I already tried with while true and for-function so I´m kinda lost… D:

yee ok thank u :smile:

You basically wrote all you need already.

private void GameOver(){
    // Put code you want to execute on Gameover
}

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.tag == "Player")
    {
        Destroy(collision.gameObject); // optional, i just copied what you had
        GameOver();
    }
}

Put that into a “KillOnHit” script and add that script to any obstacle which should cause instant gameover.
The obstacle you attach this to only need to be able to collide with things, ie needs a rigidbody.

Once you gathered a bit more experience, i would rather make these cause damage to the player and have the player handle its own death including triggering the gameover stuff, but this works too.

1 Like

Ok thanks, I´ll try with that Method, thank you :smile: