Unity 3D Load and Add Clip from folder C# script - c#

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.

Related

How to set animation during runtime in Unity using scripts

I'm not an expert at unity, so bear with me. I want to load a 3D model and apply an animation to it at runtime by using the file path. They are both FBX files and I've already been able to load the 3D model in the scene using an FBXImporter in the following code.
public GameObject Eve;
GameObject fbx;
public static string fbxPath = /*File path*/;
// Start is called before the first frame update
void Start()
{
if (File.Exists(fbxPath))
{
fbx = ModelImporter.Importer.Import(fbxPath); //Loads the 3D model from the fbx file and makes it a gameObject
fbx.transform.parent = Eve.transform; //parenting that gameObect
}
}
Now I just need to apply the animation from the other fbx file to it but I do not know how to do that. Any Help?
ps. In case you want to replicate it, I got the FBXImporter from this link: https://github.com/yuen33/FBXImporter and I got both the 3d model and animation from mixamo.
First of all simply run it in the editor and then check in the hierarchy what components are on your imported model.
If there is no Animator or Animation component anywhere then your importer doesn't support them at all.
If there is an Animator checkout the AnimatorController of it. There should be states you can switch to via Animator.Play providing according state name.
Or if it is an Animation component you will find a list of clips you can simply Animation.Play
Edit I just tested with this and this model and your importer doesn't seem to support animations ;)

I tried to makee a prefab play a sound in Unity, but it wont play whatsoever

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

can't create and save animation file in runtime unity

I'm trying to create a new animation clip file and set it in Animation component , that will be added to a GameObject in runtime.
something like this:
Animation addedAnim = fbxObject.AddComponent<Animation>() as Animation;
everything I try doesn't work :(

Unity: Instantiating prefab with animator

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

Unity3d AudioClip played with AudioSource not working properly

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.

Categories