I'm working on a Game with Unity and now i want to implement a sound effect whenever the projectile hits an object. Somehow there is no sound playing. Here is the Code:
AudioSource sound;
void Start()
{
...
sound = GetComponent<AudioSource>();
...
}
void Destroy()
{
sound.Play();
}
You have not shown if Destroy() function is called or not. Please add a Debug.Log("Called") inside your Destroy() function and test it. If it is getting called, it could be related to the volume of the system or if the Audio component is in mute. Please check these initial things.
If it is not called then learn the difference between GetComponents.
When you attach the script to your gameobject that contains AudioComponent, write it this way:
private AudioSource sound;
void Start()
{
sound = GetComponent<AudioSource>();
sound.Play();
}
When you attach the script to a different gameObject and would like to reference the Audio gameObject to this script, write it this way:
public AudioSource sound;
void Start()
{
sound.GetComponent<AudioSource>(); //this is optional
sound.Play();
}
Don't forget to add it as reference (Drag & Drop Audio component's GameObject) in the second solution.
Related
I have an DontDestroyOnLoad audio,and i want to mute it with a toggle in the other scene(where the audio activates as a Dontdestroyonload object) How can I import/put Dontdestroyonload audio into a On Value Changed toggle that is in the other scene?
I could only imagine an implementation, based on your infos, that looks something like this:
//The audio to play
public AudioSource audio;
//The toggle
public bool play;
void Update()
{
//Play if should AND it is stopped
if(play && !audio.isPlaying)
audio.Play(0);
//Pause if should AND it is playing
else if(!play && audio.isPlaying)
audio.Pause();
}
This is just a very small EXAMPLE on how it could be realized.
The play bool would need to be manually changed
I've got a problem. I am creating a small game project where where a ball escapes from ghosts.
I am trying to write a script that plays "evil laughter" when the ball collides with a ghost.
When i added the audio upon collision code into the script, the other function which gets the player to the "Game over"-scene stops working
Does anyone know what the problem might be?
Thanks a lot in advance! (code below) <3
public class GhostScript : MonoBehaviour
public AudioSource ghostCollision; //this is the collision sound reference
public GameObject target; //this is the player or a reference for him
UnityEngine.AI.NavMeshAgent agent;
// Start is called before the first frame update
void Start()
{
ghostCollision = GetComponent<AudioSource>();
agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
if (target == null) {
target = GameObject.FindGameObjectWithTag("Player");
}
}
// Update is called once per frame
void Update()
{
agent.destination = target.transform.position;
}
public void OnCollisionEnter(Collision collision){
if (collision.gameObject.tag == "Player"){
ghostCollision.Play();
SceneManager.LoadScene("menu");
}
}
That's because as soon as you call SceneManager.LoadScene the current scene (which contains your Player, AI and AudioSource) gets lost and replaced with the new Scene. Adding a delay to the scene load should do the trick, you can use Invoke for that (or Coroutines if you're more advanced).
private void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Player")
{
ghostCollision.Play();
// Invokes the function after the AudioClip is over
Invoke("LoadScene", ghostCollision.clip.length);
}
}
private void LoadScene()
{
SceneManager.LoadScene("your-scene-name");
}
First of all I apologize that this texts looks so weird.
It's my first time to use stackoverflow
I start to learn about Unity and C#.
And today I learn about move cube in unity, gonna review the script and I think i failed.
I put script in cube1 at Hierarchy, click the solution build at C# and run at unity.
And didn't work.
public class TRAIN : MonoBehaviour
{
// return cube1 to cube. cube1 is name of cube object in unity
GameObject cube = GameObject.Find("cube1");
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//move cube1 to z-axis at speed 1
cube.transform.position += new Vector3(0, 0, 1);
}
}
How can I move cube1?
You can't call GameObject.Find() directly there, you should be getting an error in the console.
UnityException: Find is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead
Do it in the Awake() or Start() instead.
public class TRAIN : MonoBehaviour
{
GameObject cube;
void Start()
{
cube = GameObject.Find("cube1");
}
I'm using this method and tried some others as well but unable to play the particle system(i.e for bullet shells ).
public ParticleSystem particleSystem;
void Start() {
particleSystem = GetComponent<ParticleSystem>();
}
void Update() {
particleSystem.Play();
}
Do not put your play triggers in the Update() of a MonoBehaviour because it will call play on every frame.
Try something like this instead:
public ParticleSystem particleSystem;
private bool isPlaying = false;
void Start() {
particleSystem = GetComponent<ParticleSystem>();
particleSystem.Clear(); // Reset the particles
}
void Update() {
if(!isPlaying) {
particleSystem.Play();
isPlaying = true;
}
}
Using particleSystem.Clear() before calling particleSystem.Play() can also help with particle issues.
The easiest way to stop and play a particle system is to deactivate and activate the particle system game object. To do this, the controlling script must be placed in a parent or separate object.
You have to make sure it's not already playing, or you will reset it
void Update() {
if(!particleSystem.isPlaying) particleSystem.Play();
}
okay so after trouble of hours i found that the best way to manipulate the particle system i.e activate and detivate on your will is to create an empty gameobject assign the particle system you made to it and then on and off this gameobject exactly at which part in the code you want to by setActivce command. I hope it will help someone in someway
I have been having difficulty getting my movie to start playing once a certain button is hit. I can get it to work with void Start() but then the video starts playing as soon as the scene is loaded which I don't want. I have tried this for when the button is pressed to start playing the movie.
public MovieTexture myMovie;
public RawImage myLayer;
private new AudioSource audio;
public void onClick()
{
GetComponent<RawImage>().texture = myMovie as MovieTexture;
audio = GetComponent<AudioSource> ();
audio.clip = myMovie.audioClip;
myMovie.Play ();
audio.Play ();
}
The movie will show but not actually play. I'm sure I'm missing something obvious but as I am a noob at coding I can't figure out what it is. Any help would be greatly appreciated!
Make a custom method which will contain
void Play()
{
myMovie.Play ();
audio.Play ();
}
and assign it to the button component in the inspector
EDIT:
I meant, don't use onClick as you use Start etc, it's not executable like that. There are 2 ways:
1) You make a custom method (called whetever you want) e.g as I listed above and assign it to the onClick field in the inspector, press +, add a script and select a method from that script;
2)
public Button yourButton;
void Start () {
Button btn = yourButton.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
}
void TaskOnClick(){
Debug.Log ("You have clicked the button!");
}
like in the reference: docs.unity3d.com/ScriptReference/UI.Button-onClick.html
You can add a Video Player using Create -> Video -> Video Player
Set video player video clip, Render Mode as Camera Far Plane and Select camera as Main Camera
Create script as given below. Just call the playMovie() method in any button event