NullReferenceException problems.
Hello, Unity Answers. I'm having trouble with a NullReferenceException. The error reads : Object reference not set to an instance of an object. I know what this means. What I don't know is why/how I am getting the error. I've done some further research on my own but I haven't found anything practical to my issue (I'm working on a Tower Defense game if that helps).
My code is below:
#pragma strict
//BulletHit will move towards an enemy. If it hits the enemy,
//the enemy will recieve the bullets damage.
//Declaring variables ::
var bulletDmg : int;
var bulletSpeed : float = 1;
var despawnTime : float = 2;
private var bulletRB : Rigidbody2D;
//Runs on start.
function Start ()
{
bulletRB = GetComponent.<Rigidbody2D>();
Despawn();
}
//Adds a force to the bullet in the direction of the enemy.
function MoveToEnemy (towerPos : Vector3, enemyPos : Vector3) {
//forceDir is equal to the Vector3 between the enemy & tower
var forceDir : Vector3 = GetEnemyDir(towerPos, enemyPos);
bulletRB.AddForce(forceDir * bulletSpeed); //ERROR HERE
}
Note: towerPos and enemyPos are given to MoveToEnemy from the class BasicTower. I've debugged BasicTower to insure that the sent items are not null.
function GetEnemyDir:
Returns a given enemy Vector3 converted to coordinates relative to the tower.
Which line does the error point to? I'm a C# dev but to my best knowledge this code: bulletRB = GetComponent.<Rigidbody2D>();
should not work in JS (I could be wrong tho). It should be something like GetComponent(RigidBody) as RigidBody
.
Your answer
Follow this Question
Related Questions
Object reference not set message, directly after instantiation 1 Answer
Script recieves previous given parameters when instantiating. Why? 1 Answer
Network.Instantiate passing parametrs to instantiated object, unassigned reference exception 0 Answers
How to get instantiate object(clone) to get the reference of the original object with scripts? 0 Answers
Basic Unity: Can someone explain the referencing and instantiation process? 1 Answer