Search Unity

teleport affecting counter... Not a big bug but i can't solve it.

Discussion in 'Scripting' started by westergard, Sep 21, 2017.

  1. westergard

    westergard

    Joined:
    May 4, 2015
    Posts:
    91
    I'm working on the UFO 2D project. Everything works fine. Now i wanted to add some teleport zones so that my player goes somewhere else on the map... But when it collides with the teleport zone, it adds one to the count. I tried different things but i can't make it work :-(

    Any help

    Here is the script on the player:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public float vitessedujoueur;
    8.     public Text countText;
    9.     public Text winText;
    10.     private Rigidbody2D rb2d;
    11.     private int count;
    12.  
    13.     void Start()
    14.     {
    15.         rb2d = GetComponent<Rigidbody2D>();
    16.         count = 0;
    17.         winText.text = "";
    18.         SetCountText();
    19.     }
    20.  
    21.     void FixedUpdate()
    22.     {
    23.         float horizontal = Input.GetAxis("Horizontal");
    24.         float vertical = Input.GetAxis("Vertical");
    25.         Vector2 mouvement = new Vector2(horizontal, vertical);
    26.         rb2d.AddForce(mouvement * vitessedujoueur);
    27.     }
    28.  
    29.     void OnTriggerEnter2D(Collider2D other)
    30.     {
    31.         if (other.gameObject.CompareTag("PickUp"))
    32.             other.gameObject.SetActive(false);
    33.         count = count + 1;
    34.         SetCountText();
    35.     }
    36.  
    37.  
    38.     void SetCountText()
    39.     {
    40.         countText.text = "Count:" + count.ToString();
    41.         if (count >= 10)
    42.             winText.text = "GOOD WORK!!";
    43.     }
    44. }
    45.  
    46.  
    And here is the code i put on the teleport zone that leads to the other destination:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class teleportation : MonoBehaviour
    5. {
    6.     public Transform destination;
    7.  
    8.     void OnTriggerEnter2D(Collider2D other)
    9.     {
    10.         other.gameObject.transform.position = destination.position;
    11.         Camera.main.transform.position = destination.position;
    12.     }
    13. }
    thanks for any input!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    If I understand your problem correctly: Is "count" supposed to be the number of pickups your ship collects? If so, you need to put { curly brackets } around the 3 lines following your if statement inside OnTriggerEnter2D. As written, all trigger collisions (no matter what the tag of the other object) will increase your count.
     
    Suddoha likes this.
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    The following part of your code is the issue:

    Code (csharp):
    1. void OnTriggerEnter2D(Collider2D other)
    2. {
    3.     if (other.gameObject.CompareTag("PickUp"))
    4.         other.gameObject.SetActive(false);
    5.     count = count + 1;
    6.     SetCountText();
    7. }
    It's equal to this snippet:

    Code (csharp):
    1. void OnTriggerEnter2D(Collider2D other)
    2. {
    3.     if (other.gameObject.CompareTag("PickUp"))
    4.     {
    5.         other.gameObject.SetActive(false);
    6.     }
    7.  
    8.     count = count + 1;
    9.     SetCountText();
    10. }
    That's a common problem you can run into when omitting the curly brackets.

    You want to make sure it's
    Code (csharp):
    1. void OnTriggerEnter2D(Collider2D other)
    2. {
    3.     if (other.gameObject.CompareTag("PickUp"))
    4.     {
    5.         other.gameObject.SetActive(false);
    6.         count = count + 1;
    7.         SetCountText();
    8.     }
    9. }
     
  4. westergard

    westergard

    Joined:
    May 4, 2015
    Posts:
    91
    Cool guys... thanks a lot!!