touch detection within area of collider of gameobject

I want to touch a gameobject based on collider, when I touch the collider, the gameobject change their color, and when my finger leave the area of collider but my finger still touch the screen, color of gameobject back to normally, how I can do this?

 foreach (Touch touch in Input.touches)
        {
            if (touch.phase == TouchPhase.Began)
            {
                Ray ray = Camera.main.ScreenPointToRay(touch.position);
                RaycastHit hit = new RaycastHit();
                if (Physics.Raycast(ray, out hit, 1000.0f))
                {
                    if (hit.collider.gameObject.name == "1")
                    {
                        hit.collider.gameObject.renderer.material.color = Color.red;
                    }
                    if (hit.collider.gameObject.name == "2")
                    {
                        hit.collider.gameObject.renderer.material.color = Color.green;
                    }
                    if (hit.collider.gameObject.name == "3")
                    {
                        hit.collider.gameObject.renderer.material.color = Color.blue;
                    }
                    if (hit.collider.gameObject.name == "4")
                    {
                        hit.collider.gameObject.renderer.material.color = Color.yellow;
                    }
                }
            }
            if (touch.phase == TouchPhase.Ended)
            {
                Ray ray = Camera.main.ScreenPointToRay(touch.position);
                RaycastHit hit = new RaycastHit();
                if (Physics.Raycast(ray, out hit, 1000.0f))
                {
                    if (hit.collider.gameObject.name == "1")
                    {
                        hit.collider.gameObject.renderer.material.color = Color.black;
                    }
                    if (hit.collider.gameObject.name == "2")
                    {
                        hit.collider.gameObject.renderer.material.color = Color.black;
                    }
                    if (hit.collider.gameObject.name == "3")
                    {
                        hit.collider.gameObject.renderer.material.color = Color.black;
                    }
                    if (hit.collider.gameObject.name == "4")
                    {
                        hit.collider.gameObject.renderer.material.color = Color.black;
                    }
                }
            }
        }

but in my script, the gameobject back to the normal color, when I release my finger from screen…

thanks all, sorry for my bad english

There are several approaches to solve this problem. Let me start with a simple one. Put the following code inside a class that will go on each of the objects you want to select. Then, in the inspector, set the over and not over color in on each object in the Inspector.

public Color enterColor = Color.red;
public Color exitColor = Color.black;

void OnMouseEnter() {
     renderer.material.color = enterColor;
}

void OnMouseExit() {
     renderer.material.color = exitColor;
}

The second approach requires you gather a list of all your game objects that can be selected. An easy way (and it would also simplify your code) is to add a tag to all of your selectable game objects. Say you set the tag to ‘Selectable’. Then you could do:

    GameObject[] gos = GameObject.FindGameObjectsWithTag("Selectable");
    foreach (GameObject go in gos) {
        go.renderer.material.color = Color.black;
    }
    foreach (Touch touch in Input.touches)
    {
        if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved)
        {
            Ray ray = Camera.main.ScreenPointToRay(touch.position);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 1000.0f))
            {
                if (hit.collider.gameObject.name == "1")
                {
                    hit.collider.gameObject.renderer.material.color = Color.red;
                }
                if (hit.collider.gameObject.name == "2")
                {
                    hit.collider.gameObject.renderer.material.color = Color.green;
                }
                if (hit.collider.gameObject.name == "3")
                {
                    hit.collider.gameObject.renderer.material.color = Color.blue;
                }
                if (hit.collider.gameObject.name == "4")
                {
                    hit.collider.gameObject.renderer.material.color = Color.yellow;
                }
            }
        }

Note this code set all the objects to black every frame, and only those with a finger over get changes back to white. There are more efficient ways with centeral logic, but this a starting point so you can see one way to gather the objects.

And the third way is a mix of the two. The following script (inserted in a class) goes on each selectable game object:

public Color enterColor = Color.red;
public Color exitColor = Color.black;

private static Transform selected = null;
private bool isSelected = false;

void Update() {
    if (isSelected && selected != transform) {
        selected = null;
        isSelected = false;
        renderer.material.color = exitColor;
    }
}

public void SelectMe() {
    if (!selected) {
        selected = transform;
        isSelected = true;
        renderer.material.color = enterColor;
    }
}

Then the Raycast code becomes:

   foreach (Touch touch in Input.touches)
    {
        if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved)
        {
            Ray ray = Camera.main.ScreenPointToRay(touch.position);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 1000.0f) && hit.tag == "Selectable")
            {
                 hit.collider.GetComponent<SomeClassName>().SelectMe();
            }
        }

This last one uses a static variable and assumes that only 1 object can be selected at a time.

All this code is untested, so you may have an issue or two, but the concepts work.