Search Unity

Calling anothr script component makes my 2d physics not work :/

Discussion in '2D' started by krillin1986, Dec 6, 2013.

  1. krillin1986

    krillin1986

    Joined:
    Oct 3, 2012
    Posts:
    36
    Ok, so, I have an empty game object, that has my player collisions. On that, I have another empty game object (EatApplesTrigger) to detect the head of my worm. I have another empty game object, that groundCheck using the 2dplatform code to check for the ground, and finally, I have my sprite object, for doing the rendering of the player graphics, and animation (wormSprite). Well, I need a certain animation to play when the "EatApplesTrigger" collider, collides with the apples. These are my scripts

    this is on my EatApplesTrigger game object
    public class EatApple : MonoBehaviour {
    Code (csharp):
    1.  
    2.     private wormAnimation WormAnimation;
    3.     public int score = 0;
    4.     private GameObject wormSprite1;
    5.     // Use this for initialization
    6.     void Start () {
    7.         {
    8.             // Setting up references.
    9.             wormSprite1 = GameObject.Find("wormSprite");
    10.             WormAnimation = wormSprite1.GetComponent<wormAnimation>();
    11.         }
    12.     }
    13.  
    14.     void OnTriggerEnter2D(Collider2D other)
    15.     {
    16.         if(other.gameObject.tag=="Apple")
    17.             //Debug.Log (WormAnimation.eatingApple);
    18.             //WormAnimation.eatingApple = true;
    19.             Destroy(other.gameObject);
    20.         score += 10;
    21.     }
    22.  
    23.     void OnGUI()
    24.     {
    25.         GUI.Box(new Rect(10,10,100,24), "Score: "+score);
    26.     }
    27.    
    28.     // Update is called once per frame
    29.     void Update () {
    30.    
    31.     }
    32. }
    and this is on my "wormSprite" object

    Code (csharp):
    1. public class wormAnimation : MonoBehaviour {
    2.  
    3.     private Animator anim;
    4.     public string horAxis = "Horizontal";
    5.     public bool eatingApple = false;
    6.     public float eatAppleDelayTimer = 0.0f;
    7.     public float eatAppleDelayAmount = 0.3f;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         anim = GetComponent<Animator>();
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update ()
    16.    
    17.     {
    18.         anim.SetFloat ("Speed",Mathf.Abs (Input.GetAxis (horAxis)));
    19.         EatingAppleFunc();
    20.  
    21.     }
    22.  
    23. void  EatingAppleFunc()
    24.     {
    25.         if (eatingApple)
    26.         {
    27.             anim.SetBool("eatApple",true);
    28.             eatAppleDelayTimer += Time.deltaTime;
    29.             if (eatAppleDelayTimer > eatAppleDelayAmount)
    30.             {
    31.                 eatingApple = false;
    32.                 eatAppleDelayTimer = 0;
    33.             }
    34.            
    35.         }
    36.         else
    37.         {
    38.             anim.SetBool("eatApple", false);
    39.         }
    40.        
    41.         if (Input.GetKeyDown(KeyCode.Q))
    42.         {
    43.             eatingApple = !eatingApple;
    44.         }
    45.     }
    46.  
    47. }
    as you see in the first script in the "OnTriggerEnter2D" function, there are 2 lines i had to comment out.
    If I comment these 2 lines out, everything else works perfectly fine, aside from the playing that eating animation i want to play. If i uncomment those 2 lines, heck, even just the Debug.Log line, all my objects that have 2d physics just fall... I am just kinda grasping the concept of using the GetComponent stuff, but, it appears that it's the cause of my problem. Anyone else experiencing this? Or does anyone have a better solution? Thanks!