Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

[SOLVED] Collision won't work

Discussion in 'Getting Started' started by BrewNCode, Feb 17, 2019.

  1. BrewNCode

    BrewNCode

    Joined:
    Feb 17, 2017
    Posts:
    372
    Hello, I'm sorry to post such newbi question, but I have a problem with my OnCollisionEnter2D function

    So, I have an Enemy_Grunt (a polygon sprite) and a Player (a triangle sprite). Both have a box collider 2D for testing purposes. I inserted this piece of code in my PlayerController script:
    Code (CSharp):
    1.     private void OnCollisionEnter2D(Collision2D col)
    2.     {
    3.         if(col.gameObject.tag == "Enemy")
    4.         {
    5.             Destroy(this.transform.gameObject);
    6.         }
    7.     }
    I have my Enemy_Grunt tagged with the "Enemy" tag, and I made sure that it was spelled correctly.
    2019-02-17 (1).png
    I actually wonder, what did I do wrong? Can someone help me, please?
     
  2. BrewNCode

    BrewNCode

    Joined:
    Feb 17, 2017
    Posts:
    372
    Ok, so I managed to figure it out, noob mistake to miss the Rigidbody2D. But, Now, I'm running another problem with the Projectile.

    I have this function in my Player controller which makes me fire up multiple instances of the projectile: (it is being called in the FixedUpdate method)

    Code (CSharp):
    1.     private void Shoot()
    2.     {
    3.  
    4.         if(Input.GetButtonDown("Fire1"))
    5.         {
    6.             GameObject projectilePrefab;
    7.  
    8.             Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    9.             projectilePrefab = Instantiate(projectile, transform.position, Quaternion.Euler(0, 0, transform.eulerAngles.z)) as GameObject;
    10.  
    11.             Rigidbody2D projectileRigidBody = projectilePrefab.AddComponent<Rigidbody2D>();
    12.             projectileRigidBody.velocity = mousePosition * projectileForce;
    13.             projectileRigidBody.gravityScale = 0.0f;
    14.         }
    15.     }
    I'm getting this error:
    Code (CSharp):
    1. Can't add component 'Rigidbody2D' to Projectile(Clone) because such a component is already added to the game object!
    2. UnityEngine.GameObject:AddComponent()
    3. PlayerController:Shoot() (at Assets/Scripts/PlayerController.cs:73)
    4. PlayerController:FixedUpdate() (at Assets/Scripts/PlayerController.cs:25)
    The line that is pointing me with the error is this one:

    Code (CSharp):
    1.             Rigidbody2D projectileRigidBody = projectilePrefab.AddComponent<Rigidbody2D>();
    In the tutorial that I'm referencing this script of for my prototype, is working perfectly well. Someone could tell me why this is happening?
     
  3. num110010

    num110010

    Joined:
    Apr 14, 2018
    Posts:
    382
    do you already have a rigidbody on the prefab?
     
  4. BrewNCode

    BrewNCode

    Joined:
    Feb 17, 2017
    Posts:
    372
    In fact I did. I was adding 2 rigidbody2d's at the gameobject. I'm going to set this at solved. Thank you.