Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Integer isn't being updated upon gameobject collision

Discussion in 'Scripting' started by CapnFedora, Aug 12, 2019.

  1. CapnFedora

    CapnFedora

    Joined:
    Jul 21, 2019
    Posts:
    3
    I have a text box that I am trying to use to keep score. To do this, I have a public int that I add 1 to whenever a collision between a bullet and an enemy occurs. However, I am not able to get the text to display the value of the integer, presumably because it is not increasing in value.

    Here is my code:

    Code (CSharp):
    1. public int intScore;
    2.    
    3.         public Collider2D PurpleSquareCollider; //these are placeholders for enemies
    4.         public Collider2D RedSquareCollider;
    5.         public Collider2D WhiteSquareCollider;
    6.    
    7.         public Collider2D BulletLeftCollider;
    8.         public Collider2D BulletRightCollider;
    9.    
    10.         public Text score;
    11.    
    12.         // Update is called once per frame
    13.         void Update()
    14.         {
    15.             Score();
    16.         }
    17.    
    18.         public void Score()
    19.         {
    20.             if (BulletRightCollider.IsTouching(PurpleSquareCollider))
    21.             {
    22.                 intScore++;
    23.                 score.text = intScore.ToString();
    24.                
    25.             }
    26.         }

    I know my code could only detect a collision between one of the bullets and one of the enemies; this is intentional. However, even that doesn't work.

    I'm new to C# as a whole and especially new to C# in Unity, so I'm not sure what I'm doing wrong.,
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    944
    Presumably your condition is always returning false. Possibly there's something wrong with your colliders. Maybe you have some other piece of code that is detecting the collision and removing the bullet and/or enemy before this code runs? Or maybe the two specific colliders you're checking just aren't touching for some reason.
     
  3. CapnFedora

    CapnFedora

    Joined:
    Jul 21, 2019
    Posts:
    3
    That's the thing. The gameobjects are colliding because in each of the gameobjects' scripts I have code to destroy them if they collide with each other. It's just that this particular script is not working.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,365
    You don't think them suddenly being destroyed might cause them to no longer be in a state of collision when the posted code comes around?

    Why not add to the score at the same time they destroy themselves?
     
    Lysander likes this.
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,598
    I would also say I hope this is a single script and not a script on each bullet or something else as each instance of the script would have it's own intScore variable.
     
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    944
    Then I will repeat myself: "Maybe you have some other piece of code that is detecting the collision and removing the bullet and/or enemy before this code runs?"
     
  7. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    4,874