Unity2D: MissingComponentException: There is no 'Animation' attached to the "Arm" game object - animation

I am trying to make an animation occur every time the space bar is pressed using this code:
using UnityEngine;
using System.Collections;
public class ArmSwipe : MonoBehaviour {
Animation me;
void Start (){
me=GetComponent<Animation>();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space)){
me.Play();
}
}
}
Every time I run it i get this error
MissingComponentException: There is no 'Animation' attached to the "Arm" game object, but a script is trying to access it.
You probably need to add a Animation to the game object "Arm". Or your script needs to check if the component is attached before using it.
UnityEngine.Animation.Play (PlayMode mode)
UnityEngine.Animation.Play ()
ArmSwipe.Update () (at Assets/Scripts/ArmSwipe.cs:18)
EDIT:

Your GameObject Arm needs to have an Animation attached to it. It appears that you don't have such an Animation attached to the Arm. Can you attach a screenshot of your scene graph, and your Arm object's inspector properties?
Also, you probably should use Input.GetKeyUp instead of KeyDown like you currently have. KeyDown will simply cause the animation to re-play on each update, and it will look like it's frozen or stuttering.

Related

unity 2D touch event blocked by something

I have my project setup like this:
Main Camera
Physics 2D Raycaster
Event Mask: Everthing
Canvas
Graphic Raycaster
Blocking Objects: None
Blocking Mask: Nothing
2 Objects setup:
GameObject
Sprite Renderer
Rigidbody 2D
Circle Collider 2D
(my GO script)
UI
Image
Button
(my UI script)
In both my GO and UI script, I add OnPointerEnter event. and both work fine on their own. I can receive OnPointerEnter event.
But when I use a joint to drag a GO object and move to top of the UI object. My UI object OnPointerEnter blocked by the GO. I cannot receive the UI OnPointerEnter event.
I search on web and everybody ask for blocking raycast on GO to UI. But I need the reverse, I want both GO and UI receive OnPointerEnter event no matter if they overlap or not. Any hints?
P.S. something like this in 2D version, GameObject block UI Object. But I still want to receive UI Object OnPointerEnter.
Finally get what I want. Now I have 2 solutions:
using OnTriggerEnter2D
turn off Physics2DRaycaster LayerMask GO when dragging GO
1. using OnTriggerEnter2D
When drag GO. Shoot event to UI and tell which GO is dragging.
Add rigibody2D(isKinematic and Freeze xyz) and 2Dcollider(is trigger) as component in UI object.
OnTriggerEnter2D receive my GO collider and check if it is the dragging GO.(doing this because I excatlly want the UI get my only dragging GO).
2. turn off Physics2DRaycaster LayerMask GO when dragging GO
use this code:
using UnityEngine.EventSystems;
public void turnOffLayerMask(string layerMaskName)
{
Physics2DRaycaster p2drc = Camera.main.GetComponent();
LayerMask layerMask = p2drc.eventMask;
LayerMask disableLayerMask = 1 << LayerMask.NameToLayer(layerMaskName);
p2drc.eventMask = layerMask & ~disableLayerMask;
}
public void turnOnLayerMask(string layerMaskName)
{
Physics2DRaycaster p2drc = Camera.main.GetComponent();
LayerMask layerMask = p2drc.eventMask;
LayerMask enableLayerMask = 1 << LayerMask.NameToLayer(layerMaskName);
p2drc.eventMask = layerMask | enableLayerMask;
}
turn off GO layerMask when dragging GO. Turn On back when drag end. The raycast can go through GO to UI and receive OnPointerXXX events.
I think the EventSystem auto. choose EITHER Physical raycast or Graphic raycast to detect GO/UI objects. So you can only receive either one set of event(Non-UI/ UI). Is it correct?
It seems that I search on web and many people using OnMouseXXX (or other method) instead of Event system(i.e. OnPointerXXX). So they can "touch through" GO to UI.

How to animate this imported character in Unity?

I imported a zombie character asset from the unity store and it came with one animator controller and two animation scripts. I already have my movement script attached to my zombie player and it moves when I press the arrow keys. I want it to do the run animation while it goes forward.
I added the animator component to my player and fed it with the animator controller which came with the asset. But when I click play I only see the idle standing animation and not the running one. do I need to use additional scripts?
You need to call the methods in the Update method, something like this:
void Update() {
if(Input.GetButtonDown("W")||Input.GetButtonDown("S")){
Run();
}else{
OtherIdle();
}
}
Using input
You need to:
1. Detect input every frame (so in your Update() method).
2. Call the methodRun() to set the animation bool to true.
This will make the Run animation play when the "w" key is pressed if the Animator Controller is set up properly:
void Update () {
// If the forward key is pressed...
if (Input.GetKeyDown("w")){
// Play the Run animation
Run();
}
}
Using speed
Alternatively you could give a new parameter to your animation controller "speed" and set the bool for the animation to play when speed is above a certain value. Else if the speed is below the value, play the Idle animation. This way, if you have a Walk animation, you can play it when the speed is between running speed and stationary.

Unity 3D Animation fbx

Please see this image
I make a script called reload :
#pragma strict
function Update () {
if (Input.GetKey (KeyCode.R)){
animation.Play("Take 001");
}
}
But when I press R :
MissingComponentException:
There is not "Animation" attached to the "AK" game object, but a script is trying to access it.
Please help
You need to add an Animation component. If you click the "Add Component" button, and search for Animation, you'll see the component you need to add.
There is difference between "Animator" and "Animation" component.
In order to use animation.play() you need to have an Animation component where the clip would be added(here the clip name should be "Take 001"). This is he legacy animation system. You need to import your fbx as legacy.
Here in your screenshot, its the Animator component that is used which takes the controller. Its the mecanim animation system.

XNA Displaying splash screen while content is loading

I'm trying to include splash screen into my XNA project while content is loading. I tried few tutorials but none has helped me. I was trying load content in update method, forcing draw method etc. Since I'm doing my project on Windows PC only I'm looking for a solution to display splash screen (propably animated) in the center of desktop. When content is loaded splash screen should disappear and the game screen should pop up.
I'm looking for some advices and tips how do it since I can't find any help on the Internet.
Personally I was thinking about using Forms but I don't know it well to use it properly.
Thanks in advance.
Since you are loading content, you are probably making a method call that spans over many frames. If you make that call on the GUI thread, you will freeze the game, which isn't what you want.
Instead, call the method inside a Task, and set your game's state as 'loading', and when the task is done, set a state that indicates the task is done.
Then, in your drawing method, you can do something like this:
if (state == State.Loading)
// draw loading screen
else if (State == .....
Extending on what Kendall Fray said, You could use game states like so:
public static GameState currentGameState = GameState.Loading;
public enum GameState
{
Loading, Whatever
}
and on Update or draw:
switch (currentGameState)
{
case (GameState.Loading):
//draw load screen
break;
case (GameState.Whatever):
break;
}

how to detect touch on a sprite in windows phone 7?

Is there any touches began function in windows phone XNA?
Here I am using a Texture2D and I want to detect the tap on it.
How to do that in xna?
You need to use the TouchCollection class and set a TouchLocation for your Texture2D
Get it's state via:
TouchCollection touch = TouchPanel.GetState()
Then iterate over the TouchLocations in your collection, i guess this will be your texture2D's position:
foreach(TouchLocation tl in touch)
Then inside this loop you can check if the location was touched via the State property of tl e.g.
if(tl.State == TouchLocationState.Pressed)
{
//Execute your code here
}
We wrote our own button class and made certain sprites inherit from this button class. Pretty good way to go about it since the entire sprite then acts as a button.

Resources