how to call a gameobject in one function(mouse selection) to another function (GUI)

im totally a newbie for unity.
i have 2 function.

1)detect the object that i selected by mouse.

2)change the texture of the object which i selected by mouse.

when i play, two function work perfectly, but when i combine both function together the object from mouse detect function–>***Upadate()***cannot be call by OnGUI() function. how to call gameobject? please help. thanks.

coding something like

function OnGUI() {

    if (GUI.RepeatButton(Rect(10,430,50,50),btnTexture1))
     {   Debug.Log("Texture changed");
     Update();
        object.renderer.material=texture1;
		lamp1.renderer.material=texture1;
		
		}
 
}

function Update(){

if (Input.GetMouseButtonDown(0)){

    var hit: RaycastHit;

    var ray = Camera.main.ScreenPointToRay(Input.mousePosition);

    if (Physics.Raycast(ray, hit)){

        point = hit.point;

        object = hit.transform.gameObject;

    }
}

else 
if (Input.GetMouseButtonUp(0)){

    duration = Time.time - startTime;
}

}

In your code, seems there are two mistakes:

  1. You should NOT call Update() function yourself. (It will be called automatically by Unity)
  2. object is a reserved word. So use ‘obj’ or any other word for that data member (variable) and try again.