- Home /
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 :) thanks
Answer by AlucardJay · Jul 02, 2012 at 08:02 PM
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);
}
}
Answer by AusAndrew19 · Jul 03, 2012 at 08:11 AM
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.
did you check the edit of my answer? (am $$anonymous$$ $$anonymous$$ay)
Oh mate thank you! i didnt see that all. Sorry :) Thank you heaps. This works completely. thank you buddy :)
np. just remember to use comments, and mark an answer that works for future readers (click on the Tick under the thumbs, next to the answer that works)
Answer by AusAndrew19 · Jul 03, 2012 at 08:42 AM
could you help me put it in a class? Im confused.
@AusAndrew19 , please use the [add new comment] button to add comments.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Teleport enemy after player collides with an object? 0 Answers
How to make a cooldown on a script 1 Answer
When do we need to use Class? 1 Answer
hide object start script 4 Answers