- Home /
NullReferenceException: Object reference not set to an instance of an object script_bullet.OnTriggerEnter (UnityEngine.Collider other) (at Assets/script_bullet.js:35)
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);
}
Did you set the asteroid - Transform in the Editor view by dragging a Transform on it? To check this go to you gameobject on which you did attach the script and then look in the list below the script name for asteroids if after asteriods there stands None(Transform) then you need to drag a Transform (a model, prefab, ....) onto asteroids.
Answer by whydoidoit · Nov 24, 2012 at 10:56 PM
Ok so firstly don't put code outside a function in Unity script - put the
asteroid = GetComponent(script_asteroid);
Inside start.
Next - what is script_asteroid attached to ? Because at the moment it appears it thinks its attached to your bullet - which seems unlikely. The GetComponent is looking at the GameObject it is attached to and trying to get the asteroid component from that - which is the bullet.
It appears CreateAsteroid is actually on the player - so you need to get a reference to that. Easiest way might be a singleton on the player - like this:
//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;
public static var instance : Player; //Presuming it's called Player!
function Awake()
{
instance = this;
}
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);
}
The any time you like you can do:
Player.instance.CreateAsteroid();
We've put together some tutorials on this stuff on Unity Gems
This solved it. Thank you for your help. $$anonymous$$y comments were muffed up so that is the asteroid script and not the player script.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Script not working (Java script) 2 Answers
Access components in ALL children 1 Answer
Official Unity Space Shooter Tutorial 1 Answer
"Absolute URI is too short" woe 1 Answer