Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Shark-Boy · Jul 31, 2014 at 01:29 AM · object pool

"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!!!

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Loius · Jul 31, 2014 at 03:46 AM 0
Share

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"

avatar image DajBuzi · Jul 31, 2014 at 06:24 AM 0
Share

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)
avatar image Shark-Boy · Aug 01, 2014 at 01:33 AM 0
Share

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.

avatar image RedSuinit · Aug 01, 2014 at 01:54 AM 0
Share

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?

avatar image Shark-Boy · Aug 02, 2014 at 05:53 PM 0
Share

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?)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Shark-Boy · Aug 01, 2014 at 10:17 PM 0
Share

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.

avatar image Kiwasi · Aug 02, 2014 at 02:42 AM 0
Share

What's happening in the scripts on your bullet?

avatar image Shark-Boy · Aug 02, 2014 at 05:42 PM 0
Share

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.

avatar image
0

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'.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Shark-Boy · Aug 02, 2014 at 05:46 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't fire a projectile from an object pool 1 Answer

How to object pool an Explosion 2 Answers

Object pooling vs Instantiate for relatively small amount of objects 0 Answers

Missile GameObject being destroyed automatically! 1 Answer

How do you call child in group of elements? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges