Sorry I’m quite new to Unity3D Could anyone tell me what is wrong with this script and why it wont work (even if I left click in unity)?
#pragma strict
function Start() {
if(Input.GetButtonDown("Fire1")){
transform.Translate(Vector3.forward);
yield WaitForSeconds(3);
transform.Translate(Vector3.back);
}
}
The “start” function is only called “once”, at the beginning of the game/level.You need to check for your keyboard input continuously. The “Update” function is called every frame.
Solution: Replace “Start()” with “Update()”
UPDATE
This is what you want:
#pragma strict
function Update()
{
BallMove();
}
function BallMove()
{
if(Input.GetButtonDown("Fire1"))
{
transform.Translate(Vector3.forward);
yield WaitForSeconds(3);
transform.Translate(Vector3.back);
}
}
There’s a couple strange things going on here.
First, function Start() is a function expected in monobehaviour objects, it executes when the scene loads before the first update frame. This code should probably be in update, or even fixedupdate depending on what you’re really trying to do.
Next, yield WaitForSeconds(3); is a feature of coroutines allowing you to pause execution, this cannot be used in update as you cannot pause execution of your update frame (well, never say never, but you wouldn’t want to).
Beyond that, I don’t really know what you’re trying to do with this script.