Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Resolved How to avoid using same script on prefabs?

Discussion in 'Getting Started' started by User_Unity90, May 16, 2024.

  1. User_Unity90

    User_Unity90

    Joined:
    Jan 12, 2024
    Posts:
    2
    I have a script that makes an object out of every pixel in an image. I'm using a 64x64 image and I get about 3000 objects (transparent pixels are ignored), and each has a script that tracks collision with the character. Contact with the character, the script destroys the object.
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class BubbleDestroy : MonoBehaviour
    5. {
    6.     // Start is called once before the first execution of Update after the MonoBehaviour is created
    7.     void Start()
    8.     {
    9.        
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.        
    16.     }
    17.  
    18.     private void OnCollisionEnter2D(Collision2D collision)
    19.     {
    20.         if (collision.gameObject.CompareTag("Player"))
    21.         {
    22.             Destroy(gameObject);
    23.         }
    24.     }
    25.  
    26.  
    Is it normal to have so many identical scripts in a project, maybe there are better ways?
    Give me a hint what can be done about this.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,363
    You can have as many or as few instances of any script you like.

    If you're having a performance issue, start with the profiler (Window -> Analysis -> Profiler).

    If it's something else you are trying to ask here...

    How to report your problem productively in the Unity3D forums:

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

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?
     
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,556
    I would rethink anything that wanted a whole collider per pixel. Consider your own pixel-collision scheme using a boxmarching technique (or even Bresenham's Algorithm). Pixels in a bitmap are a good example of the "flyweight object" pattern. They're just entries in an array, with no extra overhead of classes, references, fields, etc.
     
    Ryiah and spiney199 like this.
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,683
    The simplest change is to use a
    string
    to specify the tag on a per-object basis. With this change you now only need a single instance of this script for every tag you want to compare against and perform this action on.
    Code (csharp):
    1. public class DestroyOnCollision : MonoBehaviour
    2. {
    3.     public string tag;
    4.  
    5.     private void OnCollisionEnter2D(Collision2D collision)
    6.     {
    7.         if (collision.gameObject.CompareTag(tag))
    8.             Destroy(gameObject);
    9.     }
    10. }
     
    Last edited: May 17, 2024
  5. User_Unity90

    User_Unity90

    Joined:
    Jan 12, 2024
    Posts:
    2
    Thanks for answers. I tried several different methods. now using a simple script attached to character

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Spike : MonoBehaviour
    4. {
    5.     private void OnTriggerEnter2D(Collider2D collision)
    6.     {
    7.         if (collision.gameObject.CompareTag("Bubble"))
    8.         {
    9.             Destroy(collision.gameObject);
    10.         }
    11.     }
    12. }
    13.