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.
  2. Dismiss Notice

my box collider won't work

Discussion in '2D' started by dcruz26, Jul 18, 2016.

  1. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    a) my player cant die on the game b) can't input a score c) wont load scene when the player is on collision
    my columns are already tagged as enemy
    there is a box colllider in the prefab in the middle of the column to get a point
    when it hits enemy it wont load to the score scene/scene 2

    Code (CSharp):
    1. public class Player : MonoBehaviour
    2. {
    3.  
    4.     public Vector2 jumplength = new Vector2(0f, 300f);
    5.     public float rightspeed = 3f;
    6.     // Use this for initialization
    7.     void Start()
    8.     {
    9.        
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.         if(9>2){
    16.             transform.Translate(Vector2.right * rightspeed*Time.deltaTime);
    17.         }
    18.         if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
    19.         {
    20.             GetComponent<Rigidbody2D>().velocity = Vector2.zero;
    21.             GetComponent<Rigidbody2D>().AddForce(jumplength);
    22.          
    23.         }
    24.  
    25.     }
    26.     void Die()
    27.     {
    28.         SceneManager.LoadScene(2);
    29.     }
    30.  
    31.     void OnCollisionEnter2D(Collider2D player)
    32.     {
    33.         if (player.gameObject.tag == "enemy")
    34.         { Die(); }
    35.     }
    36. }
    37.  
    this is the code of game object with the box collider
    void onTriggerEnter2D(Collider2D collider)
    {
    if (collider.gameObject.tag == "Player")
    {
    score.addPoint();

    }
    }

    }

    this is the code for the GUI text
    public class score : MonoBehaviour {


    static int result = 0;
    static int highscore = 0;

    static public void addPoint()
    {
    result++;
    if (result > highscore)
    {
    highscore = result;
    }
    }

    // Update is called once per frame
    void Update () {
    GetComponent<GUIText>().text ="" +result;

    }

    }
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    31. void OnCollisionEnter2D(Collider2D player) should be with Collision2D. void OnCollisionEnter2D(Collision2D player)

    void onTriggerEnter2D(Collider2D collider) change to: void OnTriggerEnter2D(Collider2D collider).

    And do you have enabled trigger checkbox for OnTriggerEnter2D on boxcollider ?
     
  3. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    upload_2016-7-18_14-54-15.png
    that is my box collider and i have changed things you said but it still won't add points and die when it collides with other objects :(
     
  4. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    box collider will not work with OnTriggerEnter2D. You should use BoxCollider2D.
     
  5. Stef_Morojna

    Stef_Morojna

    Joined:
    Apr 15, 2015
    Posts:
    289
    Also for it to work, 1 of the 2 colliders that you want to collide must have a rigidbody2d ( Set the rigidbody2d to kinematic so it wont be affected by physics etc)
     
  6. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    Code (CSharp):
    1. public class scorepoint : MonoBehaviour
    2. {
    3.     void OnBoxCollider2D(Collider2D collider)
    4.     {
    5.         if (collider.gameObject.tag == "Player")
    6.         {
    7.             score.addPoint();
    8.            
    9.         }
    10.     }
    11.  
    12. }
     
  7. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    like that?
     
  8. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    No, the script should be: void OnTriggerEnter2D.
    On your gameobject "scoreinput" you have added box collider. You cannot mix 2d with 3d physic. Replace it with box collider 2d.

    - make player: add box collider 2d, rigidbody 2d
    - make scoreinput: add your script, box collider 2d (set it to trigger)

    in script
    Code (CSharp):
    1. public class scorepoint : MonoBehaviour
    2. {
    3.     void OnTriggerEnter2D (Collider2D collider)
    4.     {
    5.         if (collider.gameObject.tag == "Player")
    6.         {
    7. //add line for test, you shoud see it in log if all works.
    8. Debug.Log ("worked");
    9. score.addPoint();
    10.         }
    11.     }
    12. }
     
    Last edited: Jul 19, 2016