I am getting an Null Reference Exception when trying to call CreateAsteroid() (which is inside my script_asteroid file) from within script_bullet. I am not exactly sure how I should be calling the asteroid. Can somebody show me what I am doing wrong? Thanks in advance.
//Bullet Script
#pragma strict
//Inspector Variables
var bulletSpeed : float = 40.0; //Bullet Speed
var bulletBounds : float = 15; //Bullet Boundry
var asteroid : script_asteroid;
asteroid = GetComponent(script_asteroid);
var explosion : Transform;
function Start () {
}
function Update () {
var moveBullet : float = bulletSpeed * Time.deltaTime;
//Moves Player based on stored input data
transform.Translate(moveBullet, 0,0);
if(transform.position.x >= bulletBounds)
Destroy(gameObject);
}
function OnTriggerEnter (other : Collider)
{
//Print what the bullet is colliding with
print("bullet collison: " + other.gameObject.tag);
//Check for collision with asteroid
if(other.gameObject.tag == "Asteroid")
{
asteroid.CreateAsteroid();
//Create the explosion on impact
Instantiate(explosion,transform.position,transform.rotation);
Destroy(other.gameObject);
Destroy(gameObject);
}
}
//Player Script
#pragma strict
//Inspector Variables
var asteroidSpeedHMin : float = 1.0; //Asteroid Horizontal Speed Min
var asteroidSpeedHMax : float = 6.0; //Asteroid Horizontal Speed Max
var asteroidSpeedV : float = 10.0; //Asteroid Vertical Speed
var asteroidBoundsH : float = 15; //Limits for Asteroid movement Horizontal
var asteroidBoundsV : float = 10; //Limits for Asteroid movement Vertical
var asteroid : Transform;
var asteroidHealth : int = 3;
//Private Variables
private var asteroidPosition;
private var asteroidRotation;
private var transH;
private var transV;
function Start () {
asteroidPosition = Vector3(transform.position.x,Random.Range(-8,8),0);
asteroidRotation = transform.rotation;
transH = -Random.Range(asteroidSpeedHMin,asteroidSpeedHMax)*Time.deltaTime;
transV = Random.Range(-asteroidSpeedV,asteroidSpeedV)*Time.deltaTime;
}
function Update () {
//Moves Asteroid based on Random numbers
transform.Translate(transH,transV,0);
if(transform.position.x >= asteroidBoundsH || transform.position.x <= -asteroidBoundsH || transform.position.y >= asteroidBoundsV || transform.position.y <= -asteroidBoundsV) {
CreateAsteroid();
Destroy(gameObject);
}
}
function CreateAsteroid() {
Instantiate (asteroid, asteroidPosition, asteroidRotation);
}