Hi, I’m very new to Javascript, so I tried starting off making an easy script. Sadly, it did not work. I’m trying to make a triggerable trail, by using a box prefab (leech_trail_cube) and cloning it whereever my object goes. Although, since this may cause a bit of lag, I added a timer (wait). When I put this into unity, I get alot of errors, like (6,11) expecting (, found ‘Start’. I don’t understand where I went wrong. Could someone please correct this? Thank you!
var leech_trail_cube : Transform;
var wait = 0;
function Update() {wait=wait+1;}
if (wait == 30 )
(function Start(){;ar leech_trail_cube = Instantiate(leech_trail_cube, Vector3 (transform.position.x, transform.position.y, transform.position.z), Quaternion.identity)})
You probably want this:
var leech_trail_cube : Transform;
var wait = 0;
function Update() {
wait=wait+1;
if (wait == 30 )
{
leech_trail_cube = Instantiate(leech_trail_cube, Vector3 (transform.position.x, transform.position.y, transform.position.z),
Quaternion.identity)
}
}
Oh, the errors… let me count the ways…
Start is a function and cannot be used within a function like that.
var leech_trail_cube : Transform;
var wait = 0;
function Update() {wait=wait+1;}
if (wait == 30 )
[COLOR="cyan"]([/COLOR][COLOR="red"]function Start(){;[/COLOR][COLOR="lime"]ar[/COLOR] leech_trail_cube = Instantiate(leech_trail_cube, Vector3 (transform.position.x, transform.position.y, transform.position.z), Quaternion.identity)[COLOR="red"]}[/COLOR][COLOR="cyan"]) [/COLOR]
OK… Cyan is a parenthesis block that should not be there… it should be … { }
red is a function… bad place to put it
green is the word “ar”… which should be “var”
Concept is that every 30 frames it should drop a cube so that you can see where you were.
Solution… Use a Coroutine to measure time, then create a cube where you were.
var leech_trail_cube : Transform;
var dropEvery = 1.0; // drop every one second
function Start(){ // Start IS a coroutine and can persist past one frame
while(true){
// drop a cube
Instantiate(leech_trail_cube, transform.position, transform.rotation);
// wait for seconds
yield WaitForSeconds(dropEvery);
} // go back to the while... and continue
}
Thank you so much, the code now works! Sorry about the “{;ar”, it’s not my lack of knowledge, just my typing. (The effect of this code is to make an triggerable trail (which can be added to a trail renderer) without causing too much lag.)