Hi,
Im working on a small 2d project right now that is sort of a twin stick shooter with bouncy bullets. I’ve managed to get the bullets to bounce by using the Vector2.reflect command and collision.normal command, everything works well and the bullets reflect as intended but I’m getting a NullReferenceException error in the way im setting the velocity of the bullets after the collision. I’m setting the velocity directly through the RigidBody2D and that is where the error is pointing to. The exact error is
“NullReferenceException: Object reference not set to an instance of an object
reflectBehaviour.OnCollisionEnter2D (UnityEngine.Collision2D collision) (at Assets/Scripts/reflectBehaviour.cs:50)”
With line 50 being “rb.velocity = reflectedDir;”
The full code is pasted below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class reflectBehaviour : MonoBehaviour {
private GameObject bullet;
public GameObject bounceAnimation;
private Rigidbody2D rb;
private Vector2 initialVelocity;
private Vector2 reflectedDir;
private int bounceCount;
public int maxBounce;
public float minSpeed;
// Use this for initialization
void Start ()
{
bullet = this.gameObject;
//rb = GetComponent<Rigidbody2D>();
rb = GameObject.FindObjectOfType<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate ()
{
initialVelocity = rb.velocity;
if (rb.velocity != Vector2.zero)
{
float angle = Mathf.Atan2(rb.velocity.y, rb.velocity.x) * Mathf.Rad2Deg;
bullet.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
if (bounceCount > maxBounce)
{
Destroy(bullet);
}
if (rb.velocity.magnitude < minSpeed)
Destroy(bullet);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "wall" | collision.collider.gameObject.tag == "shield")
{
reflectedDir = Vector2.Reflect(initialVelocity, collision.contacts[0].normal);
rb.velocity = reflectedDir;
bounceCount++;
GameObject newParticle = (GameObject)Instantiate(bounceAnimation, transform.position, transform.rotation);
Destroy(newParticle, 0.1f);
}
}
}
I’m pretty new to C# so im sure there are many mistakes, thanks for any help you can offer!
Check that the variable rb is not null.
I looks like
rb = GameObject.FindObjectOfType();
is failing to find the component.
I’m pretty sure that rb is not null, I tried changing
rb = GameObject.FindObjectOfType();
to
rb = bullet.GetComponent<Rigidbody2D<>();
but this didnt change the error. I also checked by using Debug.Log() to print the object to the console. When I use Debug.Log(rb) it prints:
Bullet(Clone) (UnityEngine.Rigidbody2D)
UnityEngine.Debug:Log(Object)
reflectBehaviour:Start() (at Assets/Scripts/reflectBehaviour.cs:21)
Also if it was null then my reflections wouldn’t work at all because
rb.velocity = reflectedDir
works exactly as intended. Is it possible for the variable to be null but still get effected by this?
Right before this line
rb.velocity = reflectedDir;
do a debug
Debug.Log(rb == null, gameobject); and see what it returns. (this way clicking on the debug will also show which gameobject it occurs on)
btw, this line
rb = GameObject.FindObjectOfType(); Wouldn’t make sense as you could find a different bullets rigidbody2d, so definitely use bullet.GetComponent instead. (GetComponent is faster generally anyways)
If the line you posted is where the null error occurs, then rb is null at the time the line is called. That’s not to say it’s always null, just that it could be getting set to null for some reason. Anyways, at this point it is just a guess and just part of normal debugging steps.
Thanks for the response and advice,
I’ve tried the Debug.Log null check in both the Start and OnCollision functions and both times it has printed false. The exact message is:
False
UnityEngine.Debug:Log(Object, Object)
reflectBehaviour:Start() (at Assets/Scripts/reflectBehaviour.cs:20)
I think I may be putting the wrong 2nd argument into the log function as it just prints (Object, Object) when I put either “gameObject” or “this” and that isnt exactly helpful to me. I’m really not sure where in this script that rb could be null since it is used so often.
After some more testing ive found that the null check returns True if I check the line before rb.velocity = reflectedDir.
Im not too sure what this means. The error only occurs on instantiation of the bullet and not at every bounce, so it can’t be due to the velocity being 0 at point of contact as I initially suspected. I’m really not sure what could be causing this.
Is your bullet colliding with something when you instantiate it and the bullet is inactive or the reflectBehavior script on it is off?
1 Like
I’ve solved it!
Yes you’re completely right about the collision. The bullet is colliding with part of the player when it is being instantiated. Ive now corrected the bullet collider as it was elongated at the back and I no longer get the error.
Thank you for the suggestions, I really appreciate the help.
Glad you got it figured out!
It’s been 4 years, many thinigs have changed in the world, But you’re still a genius!