- Home /
Lerping the GUI, problem with doing it again
Hello everyone. I wanted to make a gui for my mission objective. What I wanted was a label from top would come in the middle of the screen with duration. ( our objective would be written on it ) Then wait a bit, and then go back to the very top of the screen and disappear. I accomplished this, but when I try to change the string and make it go middle of the screen again, it does not lerp again. Let me show you ;
private var startX: float;
private var endY: float;
private var startTime: float;
private var endTime: float;
static var down : boolean;
var duration: float;
`` var duration2: float;
var myString : String;
var w = 500;
var h = 42;
function Start() {
down=true;
` `startX = Screen.width/2 -180; // we dont need to deal with this, this is my desired location for x .
endY = Screen.height /2 - Screen.height / 4;
startTime = Time.time;
yield WaitForSeconds(duration+1.7); // wait for it to go top again
endTime = Time.time;
down=false;
}
function OnGUI() {
// styles and other vars
var fracTime = (Time.time - startTime) / duration;
var fracTime2 = (Time.time - endTime) / duration2;
var yPos = Mathf.Lerp(1, endY, fracTime); // endY is the position which I want the label the go for, it'a bit upper than the middle of the screen.
var yPos2 = Mathf.Lerp(endY, -1000, fracTime2); // for going back I used this
var rect = Rect(startX,yPos, w, h); // startX is screen width /2 we dont have anything to do with it.
var rect2 = Rect(startX,yPos2, w, h); // for going back which uses yPos2
if(down) // this is true at start, after 2 seconds this becomes false so the label go back.
{
GUI.Label(rect, myString,styleObjective);
}
if(!down)
{
GUI.Label(rect2, myString,styleObjective);
}
}
Now you see the script, player spawns, I make down = true at the start. Label goes down, waits, and goes back everythings alright. Then in another script, when player reaches somewhere, I change the myString value it's okay, and make "down" value true again. But it does not work as the first one. The label just appears is the endY pos, does not lerp. I think problem is about lerping.
Any ideas ? Thanks :)
Answer by Jamora · Jul 24, 2013 at 10:31 AM
Merely setting down = true is not enough, because you do all your initialization in Start(). To get this to work, you need to call StartCoroutine(*refrenceToTheScript*.Start())
(or just StartCoroutine(Start())
if you call it from the same script) instead of *scriptname*.down = true
. There are lots of ways to get a refrence to your script... the easiest (to explain), yet not efficient, is to use GameObject.Find to find the gameobject, then get the refrence with GetComponent
EDIT: On second thought, Unityscript doesn't require StartCoroutine(), so you can just call Start normally. Also, I hope you have a copy-paste mishap in your snipppet: your two ifs at the end are exactly the same. I think one of them needs rect2.
yes I had problem about copy paste it's supposed to be rect2. And thank you it works I used send message, thanks a lot for the advice :).
Your answer
![](https://koobas.hobune.stream/wayback/20220613110738im_/https://answers.unity.com/themes/thub/images/avi.jpg)