Reputation: 11
I'm busy for a project for school wroking in unity with c#. Now i have 2 objects the 'Master' object and the "Player" object. When the master clicks on his mouse I want that a sound is spawned on the player object:
Script of the master involving sound looks like this:
using UnityEngine;
using System.Collections;
public class ClickControl : Photon.MonoBehaviour {
public AudioClip newClip;
public GameObject other;
public void Click(){
if (Input.GetMouseButtonDown (0)) {
Debug.Log (other.audio.enabled);
other.audio.clip = newClip;
other.audio.Play();}
}
public void Update () {
if (Input.GetMouseButtonDown (0)) {
Click ();
}
}
Of course there is more around it but that doesnt involve the sound. The problem i get is when i insert an audioclip in the AudioClip and the Player in the GameObject i get this message: "Can not play a disabled audio source"
I tried with code to add a new audiosource to the GameObject and still this same message. I tried to instantiate it and still the same message. I'm out of options anyone a clue? This is my first question please excuse me for the lay out.
Upvotes: 0
Views: 4591
Reputation: 3049
you should add AudiSource to your other GameObject , try to do it before running the scene and if you want to add it in code add it in Awake
because otherwise there is no audio source , also there is no need for the second Input.GetMouseButtonDown (0)
public void Click(){
other.audio.clip = newClip;
other.audio.Play();}
}
public void Update () {
if (Input.GetMouseButtonDown (0)) {
Click ();
}
Upvotes: 1