jumping not working

I saw a code online that suppose to prevent infinite jumping. It work’s at the beginning but after a while I can’t jump anymore.

public class Jump : MonoBehaviour {

public bool canjump = false;

void OnCollisionEnter(Collision other)//האם האובייקט בו הוא נוגע מוגדר כgrond
{
    if (other.gameObject.tag == "Ground")//אם כן canjum=true
    {
        canjump = true;
    }
}
void OnCollisionExit(Collision other)
{
    if (other.gameObject.tag == "Ground")
    {
        canjump = false;
    }
}
// Update is called once per frame
void Update () {
	  if (Input.GetKeyDown ("space")&&canjump==true)
    {
              transform.Translate(Vector3.up * 260 * Time.deltaTime, Space.World);
      } 
}

}
Also i don’t understand why in “OnCollisionExit” - “other.gameObject.tag == “Ground””, shouldn’t it be “other.gameObject.tag != “Ground”” because I want “canjump” to be false when the player doesn’t touch the ground. The problem is when i tried to do !=, I couldn’t jump at all.

I would like help making the code work, and explaining why I need to use == and not !=.
Thanks,

@noarok
The “Collision other” parameter on OnCollisionExit represents the collision that has left the gameobject.
Therefore whatever gamobject has LEFT your player object will be what the “Collision other” would be equal to. What line 12 is really saying is: if the tag of the gameobject that has LEFT the player is equal to “Ground”… Whereas OnCollisionEnter represents the gameobject that touched it the OnCollisionExit represents the gameobject that has left it.

As for the not being able to jump anymore: I don’t know why it stops working for you. Perhaps you are using multiple gameobjects as your ground and forgot to place the “Ground” tag on them? Although I have noticed that sometimes my player will jump to the top of the screen while sometimes it will jump half that height. In other words this script is just unpredictable. There are many other alternative scripts to not making your character infinite jump.

One of my favorite tutorials on jumping is this one: Jump And Gravity - Game Mechanics - Unity 3D - YouTube although you do have to use a character controller instead of Rigidbody, which might not be the best because Character Controller does have its own set of weaknesses.

I can’t help you with this code nor can I figure out why the jump height is so irregular all the time. So unless somebody else can figure out the reason why your code isn’t working, I suggest finding a different solution to infinite jumps.

Hope this helps!