- Home /
"Null Object" but it IS there.
I have a bullet pool that works for the player but not for my enemy. It instates the bullets as inactive gameobjects and if I go to the scene and activate it it works fine. But it gives this error:
NullReferenceException: Object reference not set to an instance of an object WeaponScript.Attack (Boolean isEnemy) (at Assets/Scripts/WeaponScript.cs:58) EnemyScript.Update () (at Assets/Scripts/EnemyScript.cs:75)
And if I check for a null object before I activate it it is null.
This is my code:
using UnityEngine;
using System.Collections;
public class WeaponScript : MonoBehaviour {
public Transform shotPrefab;
public float shootingRate = 0.25f;
private float cooldown;
public bool faceForword = false;
public int numberOfBullets = 25;
private GameObject[] bulletPool;
private int nextBullet = 0;
// Use this for initialization
void Start ()
{
cooldown = 0f;
bulletPool = new GameObject[numberOfBullets];
for (int i = 0; i < numberOfBullets; ++i)
{
bulletPool[i] = Instantiate(shotPrefab) as GameObject;
Debug.Log("Made Bullet");
}
}
// Update is called once per frame
void Update ()
{
{
cooldown -= Time.deltaTime;
}
if (faceForword)
{
var rotation = transform.rotation;
rotation.z = 0;
transform.rotation = rotation;
}
}
public void Attack(bool isEnemy)
{
if (CanAttack)
{
cooldown = shootingRate;
//if (bulletPool[nextBullet] != null)
//{
bulletPool[nextBullet].SetActive(true);
bulletPool[nextBullet].transform.position = transform.position;
bulletPool[nextBullet].transform.rotation = transform.rotation;
ShotScript shot = bulletPool[nextBullet].GetComponent<ShotScript>();
if (shot!= null)
{
shot.isEnemyShot = isEnemy;
}
// Make the weapon shot always towards it
MoveScript move = bulletPool[nextBullet].gameObject.GetComponent<MoveScript>();
if (move != null)
{
move.direction = this.transform.right; // towards in 2D space is the right of the sprite
}
nextBullet += 1;
if (nextBullet >= numberOfBullets)
{
nextBullet = 0;
}
// Debug.Log("Fire");
//}
}
}
/// <summary>
/// Is the weapon ready to create a new projectile?
/// </summary>
public bool CanAttack
{
get
{
return cooldown <= 0f;
}
}
}
If you can help thanks!!!
Line 75 in the code you posted is a blank line. It would help to have the problem line bolded.
Null-ref usually means "something before a . is null"
I think the problem is here:
nextBullet += 1;
if (nextBullet >= numberOfBullets)
{
nextBullet = 0;
}
It seams to be fine but try changing if statement to
if (nextBullet >= numberOfBullets -1)
I changed the if statement but it is the same. As I said it works for the player. I am using the same bullet object as the player but it still won't work. I can see the bullet objects in the Hierarchy and activate them but the code says it can't find them.
Just a question, but are there actually // in your code like above at lines 56, 57, 82 and 83? I am assu$$anonymous$$g no, and that you added those to highlight the problem area?
That is commenting out the code that checks for a null object so I can get the error code.
I am wondering how should I inport the bullets? I am using Transform and then casting to GameObject for my array. This does not seem quit right. Is this the problem? (or part of it?)
Answer by Kiwasi · Aug 01, 2014 at 02:05 AM
Best way to solve a null reference error is to Debug.Log everything.
The error is pointing to line 58. Line 58 has bulletPool[nextBullet]
. Immediately before this insert Debug.Log (bulletPool[nextBullet])
. This will tell you if the bullet actually exists.
I suspect something is destroying your bullets. You can fix this by checking if the bullet is null, and then instantiate a new bullet to go in the gap.
There are other, more robust ways of object pooling that don't fail if an object is deleted.
I tried checking to see if the bullet is null right before bulletPool[nextBullet] and if it is null I instate a new bullet but the bullets vanish right away and it still comes up null.
The scripts on my bullet are fine but the weaponScript is disabled and if I enable it it stays enabled but the bullets still don't work.
Answer by hammil · Aug 02, 2014 at 03:25 AM
Anything deriving from UnityEngine.Object uses a different comparison operator than what you might expect. Essentially, one of these objects will equate to null if it has been destroyed, or - crucially - disabled.
Yes, you read right. A disabled object will equate to null, even though you can perform operations on it perfectly well.
The question now becomes: how do I really check if an object exists? You can cast it to an 'object' (lowercase 'o') first, which will use the standard .net operator.
However - if the object has been Destroyed, it won't necessarily equate to null using this method. So be cautious. If you're sure an object exists, but it might be disabled, this will work just fine.
UnityEngine.Object myObject;
if (myObject == null) // Is the object allocated, existant and active?
if ((object)myObject == null) // Is the object allocated?
If you really need to test whether an object exists, and you think it could be destroyed between the time you acquired the reference and the time you need to use it, try this:
bool objectExists = false;
try
{
var test = myObject.hideFlags;
objectExists = true;
} catch { }
If the object does exist, the code executes just fine. But if the object has been destroyed, Unity will throw an exception when you try to get its hideFlags, and the variable will stay 'false'.
I tried it and now the player can't fire. I'll try messing with it to see what I get. Thanks for the tip it looks like it might come in handy.
Your answer
