So I did (in Unity editor):
GameObject -> Create Empty
the I dragged my Avatar.cs (the main player animation MonoBehaviour) onto the gamefile.
Inside Avatar.cs I added:
public AudioClip audioClipJump;
and
// this is where the jump animation gets played
audio.PlayOneShot(audioClipJump);
Back in Unity editor, I see the component Audio Clip Jump inside GameObject.Avatar, and I drag and dropped the .wav file onto it.
Then I go into the game, and the jump animation plays, but I have no sound :(
here is a screenshot of the Unity editor:
and like I said, just the 2 lines in the C# file
Check that there is a AudioListener in the scene, probably attached to the Camera.
Try using the second argument on the PlayOneShot function, to indicate the volume.
Remember that having and AudioListener in the same GameObject containing the AudioSource doesn't work (for some reason).
If all fails, try playing the sound from another Component, which should only have the code for playing the sound.
Related
I also have a bunch of other Objects in the scene (that aren't instantiated prefabs) which can play sound perfectly like this:
shoot.Play();
where shoot is a public AudioSource, in which i atributed in the Inspector by dragging and dropping an Audiosource of the sound effect, which i made by dragging and dropping a .wav file into the hierarchy.
I couldn't do the same for the prefab, obviously, so i made the AudioSource its child like this:
The AudioSource, as the Prefabs child (imgur image link)
I then dragged and dropped in the Inspector, as the value of the public AudioSource. I tried to play the sound the same way using hitHurt.Play();, But it didnt work.
Someone told me to do it like this:
GetComponent<AudioSource>().Play();
Still won't work
¯\_(ツ)_/¯
edit: nvm fixed lolz
I am new to game development and building Endless Runner game, where Rocky(Player) can collect coins, and I applied cool sound effect when coin is being collected by player but that is not played. Here is the detail what I did so far,
First I create an Empty Object named it CollectCoin and drop coin collect sound on it.
Inspector View of CoinCollect, as can see CoinSFX is applied on AudioClip property
Coin Object look like this at inspector view and my AudioSource applied there in scripts. Also check Is Trigger checkbox
Player Inspector View
And finally my Class
public class CoinCollect : MonoBehaviour
{
public AudioSource coinFX;
public void OnTriggerEnter()
{
coinFX.Play();
this.gameObject.SetActive(false);
}
}
What and where i done wrong so far? I am stuck here since yesterday. Please help me.
There was nothing wrong with my script, and I dragged both Coin Collect Sound and Background Music at correct place, problem was with my Unity, I don't know when I muted "Mute Audio" option in unity, or its muted by default i have no idea but my problem is resolved. That is why my sound was not playing
I have two cameras in a scene and I want to switch between them without disabling them. Is it possible? If it is then can you show how it is done? Thanks
A nice way to use different cameras and manage transitions is using the Cinemachine package made by Unity. In the following animation I am switching between two Virtual Cinemachine Cameras by changing their priority.
Create a new empty scene with the default camera.
Add a sphere to the center of the scene. This will be the object we want to look at with our cameras. Maybe also add a plane below the sphere like in the image above, for orientation.
Go to Window->Package Manager. Search for Cinemachine and install it.
Select the current Main Camera gameobject in your scene and add a CinemachineBrain component.
There is a new main menu entry called Cinemachine. Select "Create virtual camera" two times. Two gameobjects with a "Cinemachine Virtual Camera" Script will be added. The CinemachineBrain component acts as a coordinator for those Virtual Cameras.
Position the two Virtual Cameras in your scene and drag the Sphere into the "Look At" property of both Virtual Cameras.
If you start the project, the first Virtual Camera will be active. You can raise the "Priority" property of the second Virtual Camera and the view will transition to the second camera. Alternatively you can disable the first Virtual Camera.
Cinemachine is very powerful. You can change the transitions between Virtual Cameras in the CinemachineBrain component. Select a different "Default Blend" or add "Custom Blends" for transitions.
View some Official Cinemachine Tutorials to unleash their full power.
A really easy way is to disabling just the Camera Component.
I had a scene with 6 cameras with different post processing , one of my AA plugins was making game crash after disabling camera object , then I tried to only disabling Camera Component.
Now it works just fine :)
the second thought is just delete and adding Camera Component to camera objects using an script.
How about changing the Tag on the cameras. Usually the mainCam has the MainCamera tag.
I am not sure if that works tho..
I'm making 2D platformer game using Unity 4.3.4 engine. I've created a simple prefab, which have two animations: "idle" and "death"(i used "animator") and script to control this animations.
And here's the problem: when i instantiating clones of this prefab, they always show "idle" animation and don't turn on "death" when needed.
pos = new Vector3 (-5, 4, 0) * TileSize;
newObject = Instantiate (Bonus, pos, Quaternion.identity) as GameObject;
But what is insteresting: i found a way to make animation work fine. Just add after instantiation one string like this:
newObject.animation["boxNew"].speed=1;
or this(or any string that trying to operate with "animaton"):
newObject.animation.enabled=true;
Of course i get exception at this string: "MissingComponentException: There is no 'Animation' attached to the "BonusBlock(Clone)" game object" . This is true, i really don't have Animation component, i have Animator. But why everything is working this way? Can anyone explain this?
The animator is really only used if your going full blast with the new mecanim/state flow animator, what you prolly want to do for a simple example would be to add a Animation (not animator) component to your prefab, and assign your 2 animations to the animations list in the inspector for the said animation component. Afterwards on the object you could use newObject.animation.Play("death"); to play the death animation when you want it to trigger. or use something like newObject.animation.CrossFade("death"); for a blended animation.
Found here -> http://docs.unity3d.com/Documentation/ScriptReference/Animation.html
Unity Technologies gave the answer: Before deactivating, use a default state to reset the gameObject. Discussion on Unity3d Forum
If you can read chinese, click here
I need to load one specific animation clip from .fbx file and play it in loop.
I tried this:
GameObject go = Instantiate(Resources.Load("Animations/ubohost")) as GameObject;
ubohost.fbx is file with model and animation clip called mixiamo.com.
Then I am sure that I should add the animation to the gameobject:
Animation anim = go.AddComponent<Animation>();
And after that somehow add a clip, I tried many ways, but none works.
I know that it should be really easy, but I am not getting anywhere.
Basically I need just to load and play one animation with script.