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

OnCollisionEnter for various objects with different tags

Discussion in 'Scripting' started by maxrio, Nov 21, 2015.

  1. maxrio

    maxrio

    Joined:
    Jan 8, 2015
    Posts:
    25
    Regards. I need to know how to use OnCollisionEnter for various objects with different tags on the same script. The problem is that I have to create multiple instances of the same object and as everyone has the same script used are not performing properly.I must assign the script to a static object in the scene and this script then controls the instances that I created and that are instantiated during the game.

    Follows suit:

    Code (JavaScript):
    1. function OnCollisionEnter2D(coll: Collision2D)
    2. {
    3.     if (coll.gameObject.tag == "Pin")
    4.        GetComponent.<AudioSource>().Play();
    5.          
    6.     if (coll.gameObject.tag == "Bucket")
    7.         GetComponent.<AudioSource>().Play();
    8.  
    9.     if (coll.gameObject.tag == "Wall")
    10.         GetComponent.<AudioSource>().Play();
    11. }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    perhaps you could start with explaining what you want them to do. Your "solution" sounds a little convoluted given the code snippet...
     
  3. maxrio

    maxrio

    Joined:
    Jan 8, 2015
    Posts:
    25
    OK. When the ball touches the wall or on pins it should make a sound. What happens is that it only emits the sound with backwardness, sometimes does not emit. I am sending a graph to illustrate. The ball is instantiated a few times but is on the scene after the interaction.
     

    Attached Files:

  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    When I've had lots of objects in a scene that all need to make sounds & destroy themselves I found that I couldn't put the sounds on the objects as thee wasn't time for them to play before they were destroyed. What I did was put an empty game object in as the manager, put a script on it that held an array of the sound clips, & then called that script to play the relevant sound while the object was destroyed.
     
  5. maxrio

    maxrio

    Joined:
    Jan 8, 2015
    Posts:
    25
    I see what you mean. But in this case the objects are not destroyed. What I want to do is exactly this: Insert the script into a fixed object in the scene, the main camera for example, and from this script control the interactions between objects. I am unable to configure the OnCollisionEnter with several tags.
     
  6. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    You probably want to use
    http://docs.unity3d.com/ScriptReference/AudioSource.PlayOneShot.html
    Instead as you currently aren't telling it what sound clip to actually play.

    The manager object won't be able to detect the collisions of everything, only the objects involved can do that so they can either play the relevant clip themselves depending on what they hit or they can call a manager to play it.