- Home /
plz help how do i script/code this line
Hi I'm new to Unity, I'm having a problem with writing a line of code. I need to write If x amount of seconds have passed, move GUItext to the bottom right corner. A little stuck on how I should go about writing it. Any ideas or help of what I should do would be much appreciated.
Answer by MarkD · Oct 09, 2013 at 08:36 PM
This is very easy so you will catch up quick :) Lucky for GUI coding is that OnGUI() supports coroutines (timing)
so you could for instance write
//This will be telling the button's position
var Position:Vector2(ScreenWidth,ScreenHeight);
function OnGUI(){
If(GUI.Button ("name")Position){
your function or anything;
}
yield WaitForSeconds (Amount of seconds goes here);
//and when the time has passed here we will change the coördinates of the Position variable
Position = Vector2(ScreenWidth,ScreenHeight);
}
Now this script is probably not correct because I just made it here on the fly, but it does follow the same principle you must handle to do what you are anticipating.
@$$anonymous$$arkD - You don't want to do a yield in a OnGUI like this. You will stack up a coroutine every time OnGUI() is called (which is multiple times a frame). GUI elements use Rects to position elements, not a Vector2s (and the parameters are not syntactically correct in your GUI.Button() call). GUI elements are anchored in the upper left corner, so your position of ScreenWidth and ScreenHeight would place the button off the screen (and your initialization is not syntactically correct). Finally he is asking about GUIText, which is not a GUI element. Not sure if he really means GUIText or if he means GUI.Label(), but he is asking for GUIText.
I know it is not always convenient or possible, but consider running a brief test on the code you propose as a solution.
@robertbu, you are 100% correct, like i said I made it on the fly, I did not take time to test it, but I have used yield in multiple occasions within the GUI and never seemed to have a problem with it. Ofcourse those where small applications, maybe in an application that uses more resources it could indeed be negative.
Your answer
![](https://koobas.hobune.stream/wayback/20220613115633im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Stop Script Completely. Doesn't Execute Another Line Of Code. 1 Answer
Having problems with my timer 1 Answer
Teleporter 2 Answers
This code crashes unity, please help! 0 Answers