Q: Why variable is not increased when collision is detected? I have a float named kill and I want to increase it every time when a bullet collide with an enemy.
This is my code but it isn't working. It always remains zero.
public float kill = 0;
Text killed;

void OnCollisionEnter2D(Collision2D bullet) 
{
    if(bullet.collider.tag == "bullet")
    {               
        Destroy(gameObject);
        kill++;
    }
}

void Update() 
{
    killed = GameObject.Find("killed").GetComponent<Text>();
    killed.text = kill.ToString();
}


A: The script is attached to enemy?Seems like it should be attached to the player object. Since the kill counter should belong to the player? If it is attached to the enemy object ,you also have to add to kills before destroying the game object

A: First, don't call GameObject.Find or GetComponent frequently, like in Update, as they are expensive operations. Instead, call them as few times as you can (such as calling them once in Start) and cache the result.
Also, your kill count should by all means be an integer, unless you have a very good reason it shouldn't be.
Also, when you call Destroy(gameObject) you're basically getting rid of all of the values in the fields of the component. - So you should be storing your kill count elsewhere. You could use a singleton for this, or a static class member, or even simply just using the text component to keep track of the value - you can use int.TryParse for that.
Finally, to set the inital value of the text, you can check if it's already a number and if it isn't, set it to zero. int.TryParse can also be used for that.
Altogether:
Text killed;

void Start()
{
    killed = GameObject.Find("killed").GetComponent<Text>();
    int curKilled;
    if (!int.TryParse(killed.text, out curKilled))
    {
        // Does not already contain a number, set it to zero
        killed.text = "0";
    }
}

void OnCollisionEnter2D(Collision2D bullet) 
{
    if(bullet.collider.tag == "bullet")
    {               
        int curKilled;
        if (int.TryParse(killed.text, out curKilled))
        {
            killed.text = (curKilled+1).ToString();
        }
        else
        {
            // assume it should have been zero
            killed.text = "1";
        }
        Destroy(gameObject);
    }
}

