Increasing forward speed when level completes.

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);
	}
}

}

Since the both scripts are differently attached as components to Gameobjects you need to get the component in your variable before using it.

// public class GameController : MonoBehavior
{ 
            //Declared a variable
            private AsteroidMover _asteroid  ;

           void Start()
{
                // You need to get the component before using that variable
               _asteroid = GetComponent<AsteroidMover>() ;
}

Check this Things :

  1. Check if you have hazard Prefab in your GameController component : line 18
  2. Since your are instantiating hazard as a GameObject in line 67 ,

.

var hazardClone = (GameObject)Instantiate(hazard, hazard_position, hazard_rotation) ;

Next possible thing can be your hazard Prefab with no Rigidbody component attached.

@RobbyT15

I figured out that I was trying to update the speed in the wrong script. Instead of updating the score in the GameController script, I simply needed to check the wave count inside the asteroidMover script and update it there. Here is what my updated script looks like.

public class AsteroidMover:MonoBehaviour{
	private Rigidbody asteroid;
	public float speed;
	public GameController game_controller;
    public float new_speed;
	
	void Start(){
		asteroid = GetComponent<Rigidbody>();
		asteroid.velocity = transform.forward * speed;
        GameObject game_object = GameObject.FindWithTag("GameController");
        if(game_object != null){
            game_controller = game_object.GetComponent<GameController>();
        }
        if(game_controller == null){
            Debug.Log("Can't find game controller.");
        }

        new_speed = game_controller.wave_count;

        if(new_speed > 1){
            increaseSpeed(new_speed);
        }
        else{
            Debug.Log(speed);
        }
    }

	public void increaseSpeed(float new_speed){
        speed = speed - (new_speed * 0.5f);
        asteroid.velocity = transform.forward * speed;
		Debug.Log(speed);
	}
}