how to add a sound to this script

Hi Guys. I’m Using this teleport script

using UnityEngine;

public class Teleporter : MonoBehaviour
{
public Transform exit;
static Transform last;

void OnTriggerEnter ( Collider other )
{
    if ( exit == last )
        return;

    TeleportToExit( other );
}

void OnTriggerExit ( )
{
    if ( exit == last )
        last = null;
}

void TeleportToExit ( Collider other )
{
    last = transform;
    other.transform.position = exit.transform.position;
}

}

How would i go about adding a teleport sound to it.
I thought it would be like

var teleportsound: AudioClip;

then down the bottom i would add

audio.PlayOneShot(teleportSound);

But this does not work it says Parse error. Could this be because the script is C# and maybe the audio part isnt? please help :slight_smile: thanks

yep, the C# should be :

public AudioClip teleportSound;

audio.PlayOneShot(teleportSound);

(check your spelling of teleportSound, one has teleportsound and the other has teleportSound !)

EDIT : included into your script :

using UnityEngine;

public class Teleporter : MonoBehaviour { 

  public Transform exit; 
  static Transform last;
 
  public AudioClip teleportSound;

  void OnTriggerEnter ( Collider other )
  {
      if ( exit == last )
          return;

      TeleportToExit( other );
  }

  void OnTriggerExit ( )
  {
      if ( exit == last )
      last = null;
  }

  void TeleportToExit ( Collider other )
  {
      last = transform;
      other.transform.position = exit.transform.position;
 
      audio.PlayOneShot(teleportSound);
  }

}

I’m Getting a error at line 2. A namespace can only contain types and namespace declorations. This is how i implemented it.

using UnityEngine;

public AudioClip teleportSound;

public class Teleporter : MonoBehaviour
{
public Transform exit;
static Transform last;

void OnTriggerEnter ( Collider other )
{
    if ( exit == last )
        return;

    TeleportToExit( other );
}

void OnTriggerExit ( )
{
    if ( exit == last )
        last = null;
}

void TeleportToExit ( Collider other )
{
    last = transform;
    other.transform.position = exit.transform.position;

audio.PlayOneShot(teleportSound);
}
}

Info will be appreciated thanks.

could you help me put it in a class? Im confused.