I am trying to set up a feature in a space shooter game that works alot like the game Tetris does. With each level the user completes, the hazards fall at a faster speed. However, when making the function call I get the error
Assets/Scripts/GameController.cs(22,31): warning CS0649: Field `GameController.asteroid' is never assigned to, and will always have its default value `null'
Why would this error be sent back and how do I correctly increase the hazards’ falling speed with each passing level?
public class AsteroidMover:MonoBehaviour{
private Rigidbody asteroid;
public float speed;
void Start(){
asteroid = GetComponent<Rigidbody>();
asteroid.velocity = transform.forward * speed;
}
public void increaseSpeed(){
speed += 0.5f;
Debug.Log(speed);
}
}
public class GameController:MonoBehaviour{
public GameObject hazard;
public Vector3 hazard_values;
public int min_asteroids;
public int max_asteroids;
private int hazard_count;
public float spawn_wait;
public float start_wait;
public float wave_wait;
private int wave_count = 0;
public Text score_text;
private int score;
public Text restart;
public Text game_over_text;
private bool game_over;
private bool new_game;
private AsteroidMover asteroid;
void Start(){
game_over = false;
new_game = false;
restart.text = "";
game_over_text.text = "";
score = 0;
updateScore();
StartCoroutine(asteroidWaves());
}
void Update(){
if(new_game){
if(Input.GetKeyDown(KeyCode.Return)){
Application.LoadLevel(Application.loadedLevel);
}
}
}
IEnumerator asteroidWaves(){
yield return new WaitForSeconds(start_wait);
while(true){
asteroid = GetComponent<AsteroidMover>();
wave_count += 1;
if(asteroid != null){
if(wave_count > 1){
min_asteroids *= wave_count;
max_asteroids += min_asteroids;
asteroid.increaseSpeed();
}
}
else{
Debug.Log ("Asteroid is null");
}
hazard_count = Random.Range(min_asteroids, max_asteroids);
for(int i = 0; i < hazard_count; i++){
Vector3 hazard_position = new Vector3(Random.Range(-hazard_values.x, hazard_values.x), hazard_values.y, hazard_values.z);
Quaternion hazard_rotation = Quaternion.identity;
Instantiate(hazard, hazard_position, hazard_rotation);
yield return new WaitForSeconds(spawn_wait);
}
yield return new WaitForSeconds(wave_wait);
}
}
}