Cannot add Collider2D

hi
I have an issue where I follow precisely when he adds Rigidbody2D to the Fox on the editor I press play and I get error code
"Can’t add component ‘Rigidbody2D’ to Fox because such a component is already added to the game object!
UnityEngine.GameObject:AddComponent()
Attacker:Start() (at Assets/Scripts/Attacker.cs:14)"

I replayed the video over and over to see if I had anything different in my code for attacker.cs and fox.cs.the log would say

Nothing different but I get that error as well I tried moving on without the Rigidbody2d on the editor and
the log would say “Fox collided withGrave Stone (UnityEngine.BoxCollider2D)
UnityEngine.Debug:Log(Object)
Fox:OnTriggerEnter2D(Collider2D) (at Assets/Scripts/Fox.cs:21)”

so now I cannot get the fox to jump over the gravestone. not sure if it is one issue or two.

Fox.cs


using UnityEngine;
using System.Collections;

[RequireComponent (typeof (Attacker))]
public class Fox : MonoBehaviour {

 private Animator anim;
 private Attacker attacker;


// Use this for initialization
void Start () {
  anim = GetComponent<Animator>();
  attacker = GetComponent<Attacker>();
}
// Update is called once per frame
void Update () {	
}
void OnTriggerEnter2D (Collider2D collider) {

	Debug.Log ("Fox collided with" + collider);
	
	GameObject obj = collider.gameObject;
	
	//Leave method if not coliding with defender
	if (obj.GetComponent<Defender>()){
	   return;		   
	}
    if (obj.GetComponent<Stone>()){
        anim.SetTrigger ("jumping");
        }else{
		   anim.SetBool  ("isAttack", true);
           attacker.Attack (obj);
        
	    }
	}
	
}

and Attacker.cs


using UnityEngine;
using System.Collections;

public class Attacker : MonoBehaviour {

private float currentSpeed;
private GameObject currentTarget;


// Use this for initialization
void Start (){
	
	Rigidbody2D myRigidbody = gameObject.AddComponent<Rigidbody2D>();
	myRigidbody.isKinematic = true;
}

// Update is called once per frame
void Update () {	

 transform.Translate (Vector3.left * currentSpeed * Time.deltaTime);
}

void OnTriggerEnter2d () {
 Debug.Log (name + "trigger enter");
}

public void SetSpeed (float speed){
 currentSpeed = speed;
}
// Called from the Animator at time of attack
void StrikeCurrentTarget (float damage) {
	Debug.Log (name + "caused damage" + damage);
}
public void Attack (GameObject obj) {
 currentTarget = obj;
}

}


Thank you
Michael

Privacy & Terms