- Home /
MissingComponentException after changing Animator component.
Hi!
I'm having a wierd MissingComponentException that fixes itself efter rebooting Unity. I'll explain...
I have a player-gameobject. It's a 2d-sprite with a walking and running animation. I control this by through an animator component. I also have a main camera that has a "Follow Player"-script. It references my player gameobject to get its position and follows if it leaves a "deadzone", I guess you'd call it. At first everything works as intended, no errors. That's until I make an adjustment in the player-gameobjects animator component. For example, change a float value in a transition for at which speed it should transist to the running animation. Or any other small change it seems. Then at the next play I get this error:
MissingComponentException: There is no 'Rigidbody2D' attached to the "Player1(Clone)" game object, but a script is trying to access it. You probably need to add a Rigidbody2D to the game object "Player1(Clone)". Or your script needs to check if the component is attached before using it. UnityEngine.Rigidbody2D.get_velocity () (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/Physics2DBindings.cs:705) FollowPlayer.FixedUpdate () (at Assets/Scripts/Camera/FollowPlayer.cs:33)
And the FollowPlayer-script on the main camera component stop functioning. (This of course leads to my player running out of the view, which is annoying.) Now, everything goes back to normal if I simply restart the Unity Editor, even if I saved all changes. But that is still annoying. I can't seem to fix the issue, but I think it somehow has to do with the above mentioned reference to "Player1(Clone)". My player gameobject is present in the scene at start, but it is saved as a prefab.
What exacly is going on here? All answers are appreciated. I consider myself a beginner.
I'll also post my FollowPlayer script for reference: (moves camera and zooms out if the player is going fast enough.)
public class FollowPlayer : MonoBehaviour {
public float camDeadZoneX;
public float camDeadZoneY;
public float moveSpeed;
public float zoomOutPlayerSpeed;
public float zoomAspect;
public float zoomSpeed;
private GameObject plr;
private float normalSize;
private float maxPlrVelo;
void Start()
{
normalSize = camera.orthographicSize;
plr = GameObject.FindGameObjectWithTag(Tags.player);
}
void Update()
{
maxPlrVelo = Mathf.Max(Mathf.Abs(plr.rigidbody2D.velocity.x), Mathf.Abs(plr.rigidbody2D.velocity.y));
}
void FixedUpdate()
{
Vector3 newPosX = new Vector3(plr.transform.position.x, transform.position.y, transform.position.z);
Vector3 newPosY = new Vector3(transform.position.x, plr.transform.position.y, transform.position.z);
if(plr.transform.position.x-transform.position.x > camDeadZoneX || plr.transform.position.x-transform.position.x < -camDeadZoneX)
transform.position = Vector3.Lerp(transform.position, newPosX, moveSpeed*(Mathf.Abs(plr.rigidbody2D.velocity.x)/2f));
if(plr.transform.position.y-transform.position.y > camDeadZoneY || plr.transform.position.y-transform.position.y < -camDeadZoneY)
transform.position = Vector3.Lerp(transform.position, newPosY, moveSpeed*(Mathf.Abs(plr.rigidbody2D.velocity.y)/2f));
if((plr.rigidbody2D.velocity.y > zoomOutPlayerSpeed || plr.rigidbody2D.velocity.y < -zoomOutPlayerSpeed) || (plr.rigidbody2D.velocity.x > zoomOutPlayerSpeed || plr.rigidbody2D.velocity.x < -zoomOutPlayerSpeed))
camera.orthographicSize = Mathf.Lerp(camera.orthographicSize, normalSize+maxPlrVelo/zoomAspect, zoomSpeed*Time.fixedDeltaTime);
else
{
camera.orthographicSize = Mathf.Lerp(camera.orthographicSize, normalSize, zoomSpeed*Time.fixedDeltaTime);
if(camera.orthographicSize - normalSize < 0.01f)
camera.orthographicSize = normalSize;
}
}
}
Answer by kFOZ · Dec 13, 2015 at 02:50 PM
Change the tag on your player to something different.
I'm no pro, but this worked. Now I have to edit out 'FindWithTag("Player")' to FindWithTag("plr")' for all of my scripts...