Search Unity

  1. Unity 2019.4 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  3. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

JS to C#

Discussion in 'Scripting' started by Nixel2013, May 14, 2019.

  1. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    87
    alguien me puede ayudar a pasar este JS a C# por favor, soy nuevo en esto y mucho no entiendo, gracias

    Code (csharp):
    1. var soundFile:AudioClip;
    2.  
    3. function OnTriggerStay(trigger:Collider) {
    4.     if(trigger.GetComponent.<Collider>().tag=="Player") {
    5.         GetComponent.<AudioSource>().clip = soundFile;
    6.         GetComponent.<AudioSource>().Play();
    7.     }
    8.  
    9.     else{
    10.        
    11.         GetComponent.<AudioSource>().Stop();
    12.     }
    13. }
     
  2. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    802
    This should be it:

    Code (CSharp):
    1. AudioSource soundFile;
    2.  
    3. void OnTriggerStay(Collider trigger)
    4. {
    5.     if(trigger.CompareTag("Player"))
    6.     {
    7.         // AudioSource should be cached.
    8.         GetComponent<AudioSource>().clip = soundFile;
    9.         GetComponent<AudioSource>().Play();
    10.     }
    11.     else
    12.     {
    13.      
    14.         GetComponent<AudioSource>().Stop();
    15.     }
    16. }
     
    Nixel2013 likes this.
  3. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    6,519
    Code (csharp):
    1.  
    2.     public AudioClip soundFile;
    3.  
    4.     public void OnTriggerStay(Collider trigger)
    5.     {
    6.         if (trigger.tag == "Player")
    7.         {
    8.             AudioSource source = trigger.GetComponent<AudioSource>();
    9.             if (source == null)
    10.             {
    11.                 return;
    12.             }
    13.  
    14.             source.clip = soundFile;
    15.             source.Play();
    16.         }
    17.         else
    18.         {
    19.             AudioSource source = trigger.GetComponent<AudioSource>();
    20.             if (source == null)
    21.             {
    22.                 return;
    23.             }
    24.  
    25.             source.Stop();
    26.         }
    27.     }
    28.  
     
    TaleOf4Gamers and Nixel2013 like this.
unityunity