- Home /
Confusing Object Reference Error
So I have a script which spawns a bullet. Nothing unusual, I have scripts that spawn bullets.
Except for some reason, whenever I try to get it to work, it tells me the object reference is not set to an instance of an object, but I can't seem to figure out why.
Here is what the code looks like
void Update () {
timer = timer + 1;
if (timer == 90){
spawnbullet = A1;
Dir = ShootDown;
Fire();
}
if (timer == 180){
spawnbullet = A2;
Dir = ShootLeft;
Fire();
}
if (timer == 270){
spawnbullet = A3;
Dir = ShootRight;
Fire();
}
}
void Fire(){
Rigidbody2D Shot;
Shot = Instantiate (projectile, spawnbullet.position, Quaternion.Euler (Dir)) as Rigidbody2D; //Dir and the above ShootLeft/ShootRight are Vector3 variables
Shot.velocity = transform.TransformDirection (Vector3.forward * 5); //The error returns here.
Shot.transform.Rotate (Vector3.forward); //A similar error appears when I comment out the above line.
}
The code works fine if I comment out the Shot.velocity and Shot.transform.Rotate lines the script works fine. At the 90, 180, and 270 frame, bullets spawn at A1, A2, and A3 respectively, however they just hover in space, which won't do at all for the game i'm trying to build.
So why is it returning an error, is there an instance or some information the game is missing? The Assembly isn't returning any errors and i'm terribly confused, since I use this exact code to spawn projectiles from other enemies in the game, all of which are fully functional and operate without issue.
Answer by HarshadK · Nov 21, 2014 at 07:38 AM
You are instantiating your Shot as a rigidbody. Rigidbodies don't have transform component that is why you get that error.
What you should rather do is instantiate shot as game object and add a rigidbody component to it as below:
GameObject Shot;
Shot = Instantiate (projectile, spawnbullet.position, Quaternion.Euler (Dir)) as GameObject; //Dir and the above ShootLeft/ShootRight are Vector3 variables
Shot.AddComponent<Rigidbody2D>();
Now you can perform transform operations on your Shot game object as previous.
The assembly returns an error, as GameObject doesn't contain a definition for "velocity" which is what I need.
It is because velocity is a property of your rigidbody and not game object. Get a reference to your new Rigidbody2D component you added and then set velocity for it like:
Rigidbody2D shotRigidbody = Shot.AddComponent();
shotRigidbody.velocity = transform.TransformDirection (Vector3.forward * 5);
making it shot.rigidbody2d.velocity fixed it. However for some reason, the shots still hung there for a while. After a bit of playing around, replacing Vector3.forward with the variable Dir got the projectiles to move as I like them to.
Problem solved.
Please accept the question if it solves your problem, as otherwise it hangs out as "unanswered".
Also, variables should not be capitalized. So your variables Dir, Shot, ShootDown, and so on should be 'dir', 'shot' and 'shootDown'. This is to differentiate them from classes and methods, which should be capitalized.
Your answer

Follow this Question
Related Questions
Health System Raycast Bullets 2 Answers
Bullets fall 4 Answers
Bullet Hole sprites don't align correctly on walls 2 Answers
How do I destroy a gun bullet clone? 3 Answers
Ai can fire Infinite bullets? 2 Answers