- Home /
on mouse enter without function update
pragma strict
var power : float = 500.0;
function Start () { while (true) { !OnMouseExit() {
if(Input.GetButtonUp("Jump"))
{
rigidbody.AddForce(Vector3(0,0,power));
}
}
// do stuff here that you'd normally do in Update
yield;
}
} // this code returns errors.. i am new at coding. sorry
Answer by speedything · Oct 30, 2012 at 11:09 PM
You need to understand the difference between "start" and "update". The "Start" function only occurs once, and it doesn't span multiple frames. Update, on the other hand, runs every single frame which is why you are able to test for inputs (and other things) within it.
When you use a "While" statement you are basically saying
- Is this true - yes or no?
- Next frame
- Is this true - yes or no?
- Next frame...
Start however only happens a single time. There is no way for it to be tested in subsequent frames.
So what's the solution? If you don't want test for Jump in Update (although personally that's generally the best place for it) then you'll need a coroutine. Every frame the script will return to where it left off in the coroutine, meaning you can test for "While" statements within it.
In the Start function, call a coroutine
function Start()
{
StartCoroutine(TestForMouse());
}
function TestForMouse()
{
while(put an argument here that will stop the loop)
{
if(Input.GetButtonUp("Jump"))
{
// Do your thing here
}
yield; //This tells the coroutine to return here each frame.
}
}
If you don't break the while loop it will keep going indefinitely, however once the condition has been met the loop will finish and the coroutine need to be invoked again.
Answer by getgcs · Nov 01, 2012 at 02:13 PM
Thanks so much. I did the following instead but I didn't use while...
pragma strict
var power : float = 500.0; var object : int = 0;
function OnMouseUp() {
if (object == 1) {
rigidbody.AddForce(Vector3(0,0,power)); } } function OnMouseExit() { object = 0; } function OnMouseEnter() { object = 1;
}