Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Need help to make my script easier!

Discussion in 'Scripting' started by jagiderheld, Oct 6, 2022.

  1. jagiderheld

    jagiderheld

    Joined:
    Dec 5, 2021
    Posts:
    4
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameOverScript : MonoBehaviour
    6. {
    7.     public bool[] Destroyed = new bool[31];
    8.     BoxCollider2D Collid1;
    9.     BoxCollider2D Collid2;
    10.     BoxCollider2D Collid3;
    11.     BoxCollider2D Collid4;
    12.     BoxCollider2D Collid5;
    13.     BoxCollider2D Collid6;
    14.     BoxCollider2D Collid7;
    15.     BoxCollider2D Collid8;
    16.     BoxCollider2D Collid9;
    17.     BoxCollider2D Collid10;
    18.     BoxCollider2D Collid11;
    19.     BoxCollider2D Collid12;
    20.     BoxCollider2D Collid13;
    21.     BoxCollider2D Collid14;
    22.     BoxCollider2D Collid15;
    23.     BoxCollider2D Collid16;
    24.     BoxCollider2D Collid17;
    25.     BoxCollider2D Collid18;
    26.     BoxCollider2D Collid19;
    27.     BoxCollider2D Collid20;
    28.     BoxCollider2D Collid21;
    29.     BoxCollider2D Collid22;
    30.     BoxCollider2D Collid23;
    31.     BoxCollider2D Collid24;
    32.     BoxCollider2D Collid25;
    33.     BoxCollider2D Collid26;
    34.     BoxCollider2D Collid27;
    35.     BoxCollider2D Collid28;
    36.     BoxCollider2D Collid29;
    37.     BoxCollider2D Collid30;
    38.     // Start is called before the first frame update
    39.     void Start()
    40.     {
    41.         Collid1 = GameObject.Find("GameOver").GetComponent<BoxCollider2D>();
    42.         Collid2 = GameObject.Find("GameOver (1)").GetComponent<BoxCollider2D>();
    43.         Collid3 = GameObject.Find("GameOver (2)").GetComponent<BoxCollider2D>();
    44.         Collid4 = GameObject.Find("GameOver (3)").GetComponent<BoxCollider2D>();
    45.         Collid5 = GameObject.Find("GameOver (4)").GetComponent<BoxCollider2D>();
    46.         Collid6 = GameObject.Find("GameOver (5)").GetComponent<BoxCollider2D>();
    47.         Collid7 = GameObject.Find("GameOver (6)").GetComponent<BoxCollider2D>();
    48.         Collid8 = GameObject.Find("GameOver (7)").GetComponent<BoxCollider2D>();
    49.         Collid9 = GameObject.Find("GameOver (8)").GetComponent<BoxCollider2D>();
    50.         Collid10 = GameObject.Find("GameOver (9)").GetComponent<BoxCollider2D>();
    51.         Collid11 = GameObject.Find("GameOver (10)").GetComponent<BoxCollider2D>();
    52.         Collid12 = GameObject.Find("GameOver (11)").GetComponent<BoxCollider2D>();
    53.         Collid13 = GameObject.Find("GameOver (12)").GetComponent<BoxCollider2D>();
    54.         Collid14 = GameObject.Find("GameOver (13)").GetComponent<BoxCollider2D>();
    55.         Collid15 = GameObject.Find("GameOver (14)").GetComponent<BoxCollider2D>();
    56.         Collid16 = GameObject.Find("GameOver (15)").GetComponent<BoxCollider2D>();
    57.         Collid17 = GameObject.Find("GameOver (16)").GetComponent<BoxCollider2D>();
    58.         Collid18 = GameObject.Find("GameOver (17)").GetComponent<BoxCollider2D>();
    59.         Collid19 = GameObject.Find("GameOver (18)").GetComponent<BoxCollider2D>();
    60.         Collid20 = GameObject.Find("GameOver (19)").GetComponent<BoxCollider2D>();
    61.         Collid21 = GameObject.Find("GameOver (20)").GetComponent<BoxCollider2D>();
    62.         Collid22 = GameObject.Find("GameOver (21)").GetComponent<BoxCollider2D>();
    63.         Collid23 = GameObject.Find("GameOver (22)").GetComponent<BoxCollider2D>();
    64.         Collid24 = GameObject.Find("GameOver (23)").GetComponent<BoxCollider2D>();
    65.         Collid25 = GameObject.Find("GameOver (24)").GetComponent<BoxCollider2D>();
    66.         Collid26 = GameObject.Find("GameOver (25)").GetComponent<BoxCollider2D>();
    67.         Collid27 = GameObject.Find("GameOver (26)").GetComponent<BoxCollider2D>();
    68.         Collid28 = GameObject.Find("GameOver (27)").GetComponent<BoxCollider2D>();
    69.         Collid29 = GameObject.Find("GameOver (28)").GetComponent<BoxCollider2D>();
    70.         Collid30 = GameObject.Find("GameOver (29)").GetComponent<BoxCollider2D>();
    71.     }
    72.  
    73.     private void OnCollisionEnter2D(Collision2D collision)
    74.     {
    75.         if (collision.gameObject.tag == "Player")
    76.         {
    77.             Destroy(collision.gameObject);
    78.         }
    79.     }
    80.  
    81.     // Update is called once per frame
    82.     void FixedUpdate()
    83.     {
    84.         if (Destroyed[1] == true)
    85.         {
    86.             Collid1.enabled = true;
    87.         }
    88.         if (Destroyed[2] == true)
    89.         {
    90.             Collid2.enabled = true;
    91.         }
    92.         if (Destroyed[3] == true)
    93.         {
    94.             Collid3.enabled = true;
    95.         }
    96.         if (Destroyed[4] == true)
    97.         {
    98.             Collid4.enabled = true;
    99.         }
    100.         if (Destroyed[5] == true)
    101.         {
    102.             Collid5.enabled = true;
    103.         }
    104.         if (Destroyed[6] == true)
    105.         {
    106.             Collid6.enabled = true;
    107.         }
    108.         if (Destroyed[7] == true)
    109.         {
    110.             Collid7.enabled = true;
    111.         }
    112.         if (Destroyed[8] == true)
    113.         {
    114.             Collid8.enabled = true;
    115.         }
    116.         if (Destroyed[9] == true)
    117.         {
    118.             Collid9.enabled = true;
    119.         }
    120.         if (Destroyed[10] == true)
    121.         {
    122.             Collid10.enabled = true;
    123.         }
    124.         if (Destroyed[11] == true)
    125.         {
    126.             Collid11.enabled = true;
    127.         }
    128.         if (Destroyed[12] == true)
    129.         {
    130.             Collid12.enabled = true;
    131.         }
    132.         if (Destroyed[13] == true)
    133.         {
    134.             Collid13.enabled = true;
    135.         }
    136.         if (Destroyed[14] == true)
    137.         {
    138.             Collid14.enabled = true;
    139.         }
    140.         if (Destroyed[15] == true)
    141.         {
    142.             Collid15.enabled = true;
    143.         }
    144.         if (Destroyed[16] == true)
    145.         {
    146.             Collid16.enabled = true;
    147.         }
    148.         if (Destroyed[17] == true)
    149.         {
    150.             Collid17.enabled = true;
    151.         }
    152.         if (Destroyed[18] == true)
    153.         {
    154.             Collid18.enabled = true;
    155.         }
    156.         if (Destroyed[19] == true)
    157.         {
    158.             Collid19.enabled = true;
    159.         }
    160.         if (Destroyed[20] == true)
    161.         {
    162.             Collid20.enabled = true;
    163.         }
    164.         if (Destroyed[21] == true)
    165.         {
    166.             Collid21.enabled = true;
    167.         }
    168.         if (Destroyed[22] == true)
    169.         {
    170.             Collid22.enabled = true;
    171.         }
    172.         if (Destroyed[23] == true)
    173.         {
    174.             Collid23.enabled = true;
    175.         }
    176.         if (Destroyed[24] == true)
    177.         {
    178.             Collid24.enabled = true;
    179.         }
    180.         if (Destroyed[25] == true)
    181.         {
    182.             Collid25.enabled = true;
    183.         }
    184.         if (Destroyed[26] == true)
    185.         {
    186.             Collid26.enabled = true;
    187.         }
    188.         if (Destroyed[27] == true)
    189.         {
    190.             Collid27.enabled = true;
    191.         }
    192.         if (Destroyed[28] == true)
    193.         {
    194.             Collid28.enabled = true;
    195.         }
    196.         if (Destroyed[29] == true)
    197.         {
    198.             Collid29.enabled = true;
    199.         }
    200.         if (Destroyed[30] == true)
    201.         {
    202.             Collid30.enabled = true;
    203.         }
    204.     }
    205.  
    206. }
    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)
     

    Attached Files:

    Last edited: Oct 6, 2022
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    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 :)

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

    jagiderheld

    Joined:
    Dec 5, 2021
    Posts:
    4
    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:
     
  5. jagiderheld

    jagiderheld

    Joined:
    Dec 5, 2021
    Posts:
    4
  6. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    You basically wrote all you need already.
    Code (CSharp):
    1. private void GameOver(){
    2.     // Put code you want to execute on Gameover
    3. }
    4.  
    5. private void OnCollisionEnter2D(Collision2D collision)
    6. {
    7.     if (collision.gameObject.tag == "Player")
    8.     {
    9.         Destroy(collision.gameObject); // optional, i just copied what you had
    10.         GameOver();
    11.     }
    12. }
    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.
     
    jagiderheld likes this.
  7. jagiderheld

    jagiderheld

    Joined:
    Dec 5, 2021
    Posts:
    4
    Ok thanks, I´ll try with that Method, thank you :D