Search Unity

How to add sound effect when collision

Discussion in 'Scripting' started by gjchoxon, Sep 25, 2018.

  1. gjchoxon

    gjchoxon

    Joined:
    Sep 25, 2018
    Posts:
    1
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ObjectCollision : MonoBehaviour
    4. {
    5.    
    6.  
    7.     void OnCollisionEnter(Collision col)
    8.     {
    9.         // Recuerde cambiar el nombre de "Enemy" por el nombre de sus game objects con los que colisiona
    10.         if (col.gameObject.name == "Peon")
    11.         {
    12.             GameObject text = GameObject.Find("ScoreText");
    13.             GameScore countComponent = text.GetComponent<GameScore>();
    14.             countComponent.addPoints(10);
    15.             Destroy(col.gameObject);
    16.            
    17.  
    18.         }
    19.         else if (col.gameObject.name == "Torre")
    20.         {
    21.             GameObject text = GameObject.Find("ScoreText");
    22.             GameScore countComponent = text.GetComponent<GameScore>();
    23.             countComponent.addPoints(15);
    24.             Destroy(col.gameObject);
    25.         }
    26.                 else if (col.gameObject.name == "Caballo")
    27.         {
    28.             GameObject text = GameObject.Find("ScoreText");
    29.             GameScore countComponent = text.GetComponent<GameScore>();
    30.             countComponent.addPoints(20);
    31.             Destroy(col.gameObject);
    32.         }
    33.         else if (col.gameObject.name == "Reina")
    34.         {
    35.             GameObject text = GameObject.Find("ScoreText");
    36.             GameScore countComponent = text.GetComponent<GameScore>();
    37.             countComponent.addPoints(30);
    38.             Destroy(col.gameObject);
    39.         }
    40.     }
    41.  
    42. }
    43.  
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836