- Home /
Speed does not work?
In my game, random spawned courses will fly against the player. The courses speed gets faster and faster every 20 seconds. So for example in my script i have made a loop that replays every 20 second. In that loop, it says speed = speed+somenumber. But the courses spawn with the default speed everytime they spawn, and not with the "faster speed". For example: The default speed is 3. I want the speed to be faster and faster, so i plus it with 0.2 every 20 second. But every object spawn with the default speed 3 and then when 20 seconds have passed it gans 0.2 in speed. But i want it so when speed is 3.6 (For example) every object spawn with the speed of 3.6 and then when it gets higher every object spawns with the next speed. Hope you understand. Here is the script: ignore the "fakespeed".
#pragma strict
//this will mkae the courses move towards you
var speedplus : float = 2;
var speed : float = 3.0;
var fakespeed = 1;
function Start ()
{
while(true) {
yield WaitForSeconds(20);
speed = speed +speedplus;
fakespeed++;
Debug.Log(speed);
}
}
function Update ()
{
transform.Translate(Vector2(-speed,0) * Time.deltaTime);
if(gameObject.Find("Player").GetComponent(Player).die == true)
speed = 1;
}
Answer by tpennetta · Feb 25, 2014 at 06:38 PM
Are you attaching this script to each course object that you are trying to create? I ask this because if you spawn a new course, and this script is a component of the new course, all the values are reset to the values you have initially assigned at the beginning of this script. This script has to have a more global scope, not a copy of this script on each scene object being created.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134714im_/https://answers.unity.com/themes/thub/images/avi.jpg)