- Home /
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;
}
}
I don't quite understand your question. The only thing i see is that you try to call the Update() function which gets automatically called every frame (like the OnGUI function). So there is no need to call it manually because it already gets called pretty much permanently. Is your problem that your if's don't work?
never $$anonymous$$d. i solved it by putting them into 2 different script.. and call the "texture" to "detect function". thanks.
Answer by appearance · Apr 27, 2013 at 09:15 AM
In your code, seems there are two mistakes:
You should NOT call Update() function yourself. (It will be called automatically by Unity)
object is a reserved word. So use 'obj' or any other word for that data member (variable) and try again.
thanks, found my mistake. i solved my problem by using another method. i call the texture to mouse detect function.
Your answer
Follow this Question
Related Questions
Nearest object code not falling back on alternate in OnGUI() 3 Answers
Display the speed of a Game Object in MPH 1 Answer
Gameover function calling before game ends help 2 Answers
How do I stop an immediate collision with all objects from ocuring at the entry of game mode? 0 Answers
How to cast spells by mouse click 2 Answers