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 Party Boy · Dec 11, 2013 at 09:04 PM · objectreferencenullexceptionpool

Object Pool object reference is null?

Hey. So I'm new to the whole Unity thing and basically for a college assignment I have to assign a design pattern to my final year project. Since I'm doing a game in Unity I need to apply it to this. I was going to apply it to different spells that you can select to shoot different objects.

So I followed a YouTube tutorial on how to create a ObjectPool and I thought everything was going okay, however when I start the game, upon clicking, nothing emits, and I also get an error at the bottom saying "Object reference was not set to an instance of an object".

I created a prefab with a sphere in it. I created 2 scripts, 1 called Object Pool, the other called Projectile. My Camera has its own script called FirstPersonController that is used for movement. In the ObjectPool script I detect if the user has pushed down the mouse button, then I call the ActivatedProjectile method.

EDIT

I get 2 types of error. The first appears only once at the start of the code:

ArgumentException: The thing you want to instantiate is null. UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineObject.cs:104) UnityEngine.Object.Instantiate (UnityEngine.Object original) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineObject.cs:90) ObjectPool.InstantiateProjectiles () (at Assets/Scripts/ObjectPool.cs:28) ObjectPool.Start () (at Assets/Scripts/ObjectPool.cs:12)

The other appears multiple times as follows:

NullReferenceException: Object reference not set to an instance of an object ObjectPool.ActivateProjectile () (at Assets/Scripts/ObjectPool.cs:37) ObjectPool.Update () (at Assets/Scripts/ObjectPool.cs:20)

Here is the code I have for ObjectPool class.

 using UnityEngine;
 using System.Collections;
 
 public class ObjectPool : MonoBehaviour {
 
     GameObject[] projectiles = null;
     public int numberOfProjectilesToCreate = 0;
 
     // Use this for initialization
     void Start () {
         projectiles = new GameObject[numberOfProjectilesToCreate];
         InstantiateProjectiles ();
     }
     
     // Update is called once per frame
     void Update () 
     {
         if(Input.GetMouseButton(0))
         {
             ActivateProjectile();
         }
     }
 
     private void InstantiateProjectiles()
     {
         for (int i = 0; i < numberOfProjectilesToCreate; i++) 
         {
             projectiles[i] = Instantiate(Resources.Load("Prefabs/prefab_projectile")) as GameObject;
             projectiles[i].SetActive(false);
         }
     }
 
     private void ActivateProjectile()
     {
         for (int i = 0; i < numberOfProjectilesToCreate; i++) 
         {
             if(projectiles[i].activeSelf == false)
             {
                 projectiles[i].SetActive(true);
                 projectiles[i].GetComponent<Projectile>().Activated();
             }
         }
     }
 }
 

Anybody have any ideas on what I'm doing wrong?

Comment
Add comment · Show 3
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 stevethorne · Dec 11, 2013 at 09:23 PM 0
Share

It would help if you gave us the line number of the exception.

avatar image robertbu · Dec 11, 2013 at 09:36 PM 0
Share

What line is generating the null? Are you sure your Instantiate() is succeeding? Not your error, but it would be best to only do the Resources.Load() once in Start().

avatar image Party Boy · Dec 11, 2013 at 10:09 PM 0
Share

@stevethorne So sorry I completely forgot ha. I get errors on lines 12, 20 and 37 I believe. I updated the OP with the full error messages.

@robertbu I updated the OP with the full error codes, my bad. I'm not too sure what is working and whats not, as I said I'm very new to the unity work flow. Ah okay I will change that now thanks

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by flaviusxvii · Dec 11, 2013 at 10:09 PM

For Resources.Load("Prefabs/prefab_projectile") you need to have the folder "Prefabs" inside a folder named "Resources".

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 Party Boy · Dec 11, 2013 at 10:40 PM 0
Share

Ah I'm an idiot I had an Asset folder but didn't have them inside a Resource folder. Thanks man :)

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

17 People are following this question.

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

Related Questions

C# Null Object Reference - Driving me mad! 1 Answer

NullReferenceException on Photon Unity RPC Call 1 Answer

NullReferenceException Object reference error 1 Answer

NullRefernceExcepception with Raycasting 1 Answer

Null reference.. 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