- Home /
How can i switch on music on a Radio (object)?
I want to switch On a radio, with the music playing from the object (radio) with a raycast from my FPS, and and i already have this script:
pragma strict
creen.lockCursor = true; var camera_XX : MouseLook; var camera_YY : MouseLook; var Musica: AudioClip[];
function Start () {
}
function Update () {
var ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2,Screen.height/2));
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 1000)) {
if(hit.collider.gameObject.tag == "radio")
{
hit.collider.gameObject.audio.Play.("Umacasa");
//"Umacasa" is the name of the music file
}
}
}
but it´s does´t work. i allready put the file in object audio source.
Can somebody help?
Please format your code properly by using the button with the 1's and 0's in its icon. And what do you mean, it doesn't work? How much of the code is executed?
Answer by tanoshimi · Oct 15, 2013 at 03:00 PM
hit.collider.gameObject.audio.Play.("Umacasa");
looks like it has an extra dot in it. Try:
hit.collider.gameObject.audio.Play("Umacasa");
Answer by BarberaTp · Oct 15, 2013 at 04:35 PM
shows this error.
Assets/Scripts_Gnosias/Ligar_radio.js(23,49): BCE0023: No appropriate version of 'UnityEngine.AudioSource.Play' for the argument list '(String)' was found.
and if it´s works, the sound will be from the object or stay on the environment? i want it play from the object that i can stepway and the sound stay more far..
tnks for your help.
Please don't post comments as answers, ins$$anonymous$$d use the Add new comment button to reply to someone else's comment.
Answer by BarberaTp · Oct 15, 2013 at 04:24 PM
Finally discover!
pragma strict
creen.lockCursor = true; var camera_XX : MouseLook; var camera_YY : MouseLook; var Musica: AudioClip[];
function Start () {
}
function Update () {
if (Input.GetMouseButtonUp(1)){
var ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2,Screen.height/2));
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 1000)) {
if(hit.collider.gameObject.tag == "radio")
{
hit.collider.gameObject.audio.Play(1);
}
}
}
}
Please accept the answer as the right one, so the question is categorized as solved for other visitors.