How do i use PlayOneShot() on Unity? - c#

I've been trying to use this function to trigger an sound when entering a trigger but i can't make it work. I've read and read my code and nothing seems off, looks exactly like the documentation says so i just can't figure out my error.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class footsteps_script : MonoBehaviour
{
public GameObject soundObject;
public GameObject trigger;
private AudioClip clip;
public bool nextTrig;
void Start()
{
nextTrig = false;
clip = soundObject.GetComponent<AudioClip>();
}
void OnTriggerEnter ()
{
if (Bunny_script.nextTrig == true)
{
soundObject.GetComponent<AudioSource>().PlayOneShot(clip);
nextTrig = true;
trigger.SetActive(false);
}
}
}
The AudioSource is atached to another object. The trigger is supposed to play the sound after another event happens. The trigger part works fine because nextTrig is set to true like intended, but the sound doesn't play. Also, the sound itself works fine too and with a nice volume.

it does not work because, what is this ?
clip = soundObject.GetComponent<AudioClip>();
there is no component called AudioClip.
for audio clip get it from prefabs directly to the script or if you want to get it from the audio source that you have in the soundObject, then it will be like :
clip = soundObject.GetComponent<AudioSource>().clip;
and later you will play it with PlayOneShot which will be waste of time, because you are playing the same clip which is originally taken from this audio source. which actually need just this line to play it
soundObject.GetComponent<AudioSource>().Play();
finally if you are trying to get the audio clip in the script from the prefabs your code will be like :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class footsteps_script : MonoBehaviour
{
public GameObject soundObject;
public GameObject trigger;
public AudioClip clip;
public bool nextTrig;
void Start()
{
nextTrig = false;
//don't forget to assign the your audio clip to the script from the prefabs
}
void OnTriggerEnter ()
{
if (Bunny_script.nextTrig == true)
{
if (clip =! null)
soundObject.GetComponent<AudioSource>().PlayOneShot(clip);
nextTrig = true;
trigger.SetActive(false);
}
}
}
and if you already have the audio clip in audio source which is attached to the sound object then your code will be :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class footsteps_script : MonoBehaviour
{
public GameObject soundObject;
public GameObject trigger;
public bool nextTrig;
void Start()
{
nextTrig = false;
}
void OnTriggerEnter ()
{
if (Bunny_script.nextTrig == true)
{
soundObject.GetComponent<AudioSource>().Play();
nextTrig = true;
trigger.SetActive(false);
}
}
}

Related

Unity script work on Editor but not working in Android

Hello I'm game developer one day when I want to test my game on android I join to my game but when I press on buttons public void function doesn't work I fixed it by add to scripts what using buttons onClick.AddListener() but one problem IAP don't work with onClick.AddListener() you can help me fix problem with public void function what works on Editor but don't work in Android when I press on buttons. Thanks.
Script with AddListener [This script where I add onClick.AddListener() to fix this problem but it's be uncomfortable thats why I want to fix this]:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameButtonManager : MonoBehaviour
{
public GameObject DonationShop;
public GameObject ShopBtn;
public GameObject QuitShopBtn;
public GameObject QuitBtn;
public bool CanPress = true;
void Update()
{
ShopBtn.GetComponent<Button>().onClick.AddListener(EnableShop);
QuitShopBtn.GetComponent<Button>().onClick.AddListener(DisableShop);
QuitBtn.GetComponent<Button>().onClick.AddListener(BackToMenu);
}
public void EnableShop()
{
if(CanPress == true)
{
DonationShop.SetActive(true);
CanPress = false;
StartCoroutine(CanPressEnable());
}
}
public void BackToMenu()
{
SceneManager.LoadScene(1);
}
public IEnumerator CanPressEnable()
{
yield return new WaitForSeconds(5.5f);
CanPress = true;
}
public void DisableShop()
{
if(CanPress == true)
{
DonationShop.SetActive(false);
CanPress = false;
StartCoroutine(CanPressEnable());
}
}
}

How do I keep my music going betwen scenes and not restart in unity

My problem is that my music restarts when my player dies, I tried the "DontDesteroyOnLoad" so the music keeps playing betwen the scenes. But when my player dies and goes to the previous scene the first generated music dosent stop, it keeps going, and after the player goes to the previous scene it starts again . And it runs at the same time as the first generated one does.
This is the code I have.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DontDestroyAudio : MonoBehaviour
{
private void Awake()
{
DontDestroyOnLoad(transform.gameObject);
}
}
Create an emepty scene put all things that you want to manage during all the game cycles there. Like your music manager. Put the script with audio there and use DontDestroyOnLoad as you have written. At first, load that empty scene with your managers. And then load all your level scenes. This way you will only have one music manager for your entire game.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioManager : MonoBehaviour
{
public static AudioManager instance;
[SerializeField]
private AudioSource audioSource;
[SerializeField]
private AudioClip audioClip;
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(this.gameObject);
}
else
{
Destroy(this.gameObject);
}
}
public void PlayAudio()
{
audioSource.clip = audioClip;
audioSource.loop = true;
audioSource.Play();
}
public void StopAudio()
{
audioSource.Stop();
}
}
Function Call :
AudioManager.instance.PlayAudio();
AudioManager.instance.StopAudio();

Type for Cinemachine in Unity

So I have a freelook camera, and I want it so that it only turns when the player drags the right mouse button. The problem is, since Cinemachine is a plugin, I'm not sure what type I should set the FreeLook Camera component. I want to make something like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine.Utility;
public class lookOnDrag : MonoBehaviour
{
// Not sure what type to set CFL
private Cinemachine CFL;
void Start()
{
CFL = GetComponent<CinemachineFreeLook>();
}
void Update()
{
if (Input.GetMouseButtonDown(1))
{
CFL.enabled = true;
}
else
{
CFL.enabled = false;
}
}
}
I've checked the documentation thoroughly and don't have an answer.
You have probably entered the above reference incorrectly and it does not recognize it correctly. using only Cinemachine and delete utility.
using Cinemachine;
Also define the variable according to the component or CinemachineVirtualCameraBase;
private CinemachineFreeLook CFL;
void Start()
{
CFL = GetComponent<CinemachineFreeLook>();
}

Unity 3D: Footstep sounds looping the first few milliseconds instead of playing the full sound then looping

When I move in game, instead of my sound being played in full, it is playing just a few milliseconds on a loop.
Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Footsteps : MonoBehaviour
{
public AudioSource audioSource;
public PlayerMovement pM;
void Start()
{
audioSource.Stop();
}
void Update()
{
PlaySound();
}
public void PlaySound()
{
if (Input.GetKey(KeyCode.W))
{
audioSource.Play();
}
else
{
audioSource.Stop();
}
}
}
Video Example.
Any suggestions would be greatly appreciated!
The method Input.GetKey() is called as long as the key is pressed. To do that you want, you could use Input.GetKeyDown() like this example:
public void PlaySound()
{
if (Input.GetKeyDown(KeyCode.W))
{
audioSource.Play();
}
else if(Input.GetKeyUp(KeyCode.W))
{
audioSource.Stop();
}
}
There are other ways to to this kind of sounds, but this simple way should work.
References:
https://docs.unity3d.com/ScriptReference/Input.GetKey.html
https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html
As #Jamarin mentioned, you can use the moving keys to add the footstep sound.
I just added a list with all moving keys to give a complete solution.
using System.Collections.Generic;
using UnityEngine;
public class Footsteps : MonoBehaviour
{
List<UnityEngine.KeyCode> keys = new List<UnityEngine.KeyCode>();
void Start()
{
keys.Add(KeyCode.W);
keys.Add(KeyCode.S);
keys.Add(KeyCode.A);
keys.Add(KeyCode.D);
keys.Add(KeyCode.UpArrow);
keys.Add(KeyCode.DownArrow);
keys.Add(KeyCode.LeftArrow);
keys.Add(KeyCode.RightArrow);
}
void Update()
{
if (keys.Exists(key => Input.GetKeyDown(key)))
{
// PLUS: Change footstep sound to simulate a different kind of footstep.
GetComponent<AudioSource>().volume = Random.Range(0.8f, 1);
GetComponent<AudioSource>().pitch = Random.Range(0.8f, 1.1f);
GetComponent<AudioSource>().Play();
}
if (keys.Exists(key => Input.GetKeyUp(key)))
{
GetComponent<AudioSource>().Stop();
}
}
}
Reference:
https://www.youtube.com/watch?v=ih8gyGeC7xs

Unity can't see the attached animation

As stated in the title unity does not see the animation attached to it through the code, which in turn, this animation I put in the inspector
My player has a BoxCollider2D with IsTrigger ticked, so that it takes the value and runs
I put a check (already in the code) for the presence of animation, but he says exactly that he does not see
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CrackedPlatformScript : MonoBehaviour
{
public AnimationClip destroyed;
private void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "Player")
{
if(destroyed != null)
{
GetComponent<Animation>().Play(destroyed.name);
gameObject.GetComponent<BoxCollider2D>().enabled = false;
Destroy(gameObject,0.4f);
}
else
{
Debug.Log(destroyed.name);
}
}
}
}

Categories