Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

JS to C#

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

  1. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    143
    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:
    825
    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:
    7,462
    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.