Search Unity

My player collider keeps deleting objects I don't want deleted on impact

Discussion in 'Scripting' started by Ghostie_, May 29, 2018.

  1. Ghostie_

    Ghostie_

    Joined:
    Jan 3, 2018
    Posts:
    7
    Hello,
    Im kind of new to the whole debugging thing here, so I'm wondering if you guys could help me. I made a little flight simulator using the standard assets jet airplane. The way the game works is that you fly through 10 loops to collect 10 coins, once you collect 10 coins, the roof opens and you fly into a teleporter and teleport to a new world.

    My problem arises when I crash into a wall. It deletes the wall and allows the player to fly out of the play area. in some cases, it also deletes the teleporter leaving the player stuck in a plane instead of pregressin onto the next scene.

    Note: I think the problem is near line 33, but I dont want to just include that last part because the issue could arise further up.

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine.UI;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public float speed;
    8.     public Text countText;
    9.     public GameObject Roof;
    10.  
    11.  
    12.  
    13.     private Rigidbody rb;
    14.     private int count;
    15.  
    16.     void Start ()
    17.     {
    18.         rb = GetComponent<Rigidbody> ();
    19.         count = 0;
    20.         SetCountText ();
    21.  
    22.     }
    23.  
    24.     void FixedUpdate ()
    25.     {
    26.         float moveHorizontal = Input.GetAxis ("Horizontal");
    27.         float moveVertical = Input.GetAxis ("Vertical");
    28.  
    29.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    30.  
    31.         rb.AddForce (movement * speed);
    32.     }
    33.     void OnTriggerEnter(Collider other) {
    34.         {
    35.             Destroy (other.gameObject);
    36.         }
    37.         if (other.gameObject.CompareTag ("Pick Up")) {
    38.             other.gameObject.SetActive (false);
    39.             count = count + 1;
    40.             SetCountText ();
    41.         }
    42.     }
    43.     void SetCountText () {
    44.         countText.text = "Score:" + count.ToString ();
    45.         if (count >= 10)
    46.         {
    47.             Destroy (Roof);
    48.  
    49.         }
    50.     }
    51.                
    52. }
    53.    
    54.  
    Thanks for the help!
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You probably want to move the line that destroys the other.gameObject so it's inside the if statement about the pickup. That way, it only destroys your coins (I assume that's what you want*).
     
    Joe-Censored likes this.
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Why is the destroy call in an isolated block? What's the purpose?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Yeah like methos said, you should be checking that what you've collided with is a pickup before destroying it, rather than destroying anything you collide with.
     
  5. Ghostie_

    Ghostie_

    Joined:
    Jan 3, 2018
    Posts:
    7
    That makes sense. Thanks. Can't believe I didn't think of that.