Mass adding components to objects

so I’m not sure if I’m wording this correctly to make sense but i have a game map of hundreds of tiles and i want to add the same sound effect to each tile “TileBoop” I really don’t want to do this manually to each tile. Is there a way to add them via script or a fast way of doing this? added a photo for reference.

5908343--630827--LayoutEffects.jpg

Yes.

Yes. Actually I wouldn’t add separate components to all of the tiles. Instead I would either play the SFX when the event happens from a centralized place or create a small pool of audiosources if you need multiple SFX at the same time.

1 Like

alright. so the script i am using for each tile is to basically change the color and play the sound when the mouse hovers over it. are you saying to have the sound play from a seperate script and have that attached to a empty game object that only plays sound when, say i hover over a game object with “Tile” tag or something like that? also this is the code im using.

public class UnitLayoutTileEffects : MonoBehaviour
{

    //when mouse is over object change color to yellow
    Color m_MouseOverColor = Color.yellow;

    //stores gameobjects original color
    Color m_OriginalColor;

    //gets the gameobjects mesh renderer to access the GOs material and color
    MeshRenderer m_Renderer;

    //sound for tile boop
    public AudioSource TileBoop;

    //used to play sound once
    public bool alreadyPlayed = false;

    private void Start()
    {
        //only want to do this for tiles nothing else
        if (gameObject.tag == "Tile")
        {
            //fetch the mesh renderer component from the GO
            m_Renderer = GetComponent<MeshRenderer>();
            //fetch the original color of the GO
            m_OriginalColor = m_Renderer.material.color;
        }

    }


    void OnMouseOver()
    {
         if (gameObject.tag == "Tile" && !alreadyPlayed)
        {
            //GetComponent<Renderer>().material.color = Color.yellow;
            m_Renderer.material.color = m_MouseOverColor;
            TileBoop.Play();
            alreadyPlayed = true;
        }
    }

    private void OnMouseExit()
    {
        alreadyPlayed = false;
        if (gameObject.tag == "Tile")
        {
            m_Renderer.material.color = m_OriginalColor;
        }
    }
}

Ah, you have these game objects in editor time? Because then just select all and add the AudioSource (and set it up). And in the Start method just add the line TileBoop = GetComponent();

1 Like

Noice. almost to easy. much thanks

1 Like