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
1
Question by LeftyTwoGuns · Jun 02, 2014 at 11:50 PM · transformprojectilepoolingobject pool

Can't fire a projectile from an object pool

I originally wrote my projectile fire script using instantiate but I've learned more on object pooling and recycling and how it's much more efficient. Now I have a basic object pool that can contain multiple objects to pool and I'm having a hard time replacing instantiate with activating a free object in the pool. Here is the pool script:

     public GameObject[] objects;
     public int[] number;
 
     public List<GameObject>[] pool;
 
 
     void Start () {
 
         instantiate();
     
     }
     
 
     void instantiate(){
 
         GameObject temp;
         pool = new List<GameObject>[objects.Length];
 
         for(int count = 0; count < objects.Length; count++){
 
             pool[count] = new List<GameObject>();
 
             for(int num = 0; num < number[count]; num++){
                 temp = (GameObject)Instantiate(objects[count]);
                 temp.transform.parent = this.transform;
                 pool[count].Add(temp);
             }
 
         }
 
     }
 
     public GameObject activate(int id){
 
         for(int count = 0; count < pool[id].Count; count++){
 
             if(!pool[id][count].activeSelf){
 
                 pool[id][count].SetActive(true);
                 return pool[id][count];
 
             }
         }
 
         return null;
     }

That's attached to an empty game object where I also plug in the bullet prefab in the objects list and set the amount I want. Here is my shoot script which I tried to modify for the new object pool:

     public ObjectPooler pool;
 
     private Transform myTransform;
 
     private Transform cameraHeadTransform;
 
     private Vector3 launchPosition = new Vector3();
 
     public float fireRate = 0.2f;
 
     public float nextFire = 0;
 
     
     void Start () {
     
         myTransform = transform;
 
         cameraHeadTransform = myTransform.FindChild("BulletSpawn");
 
     }
     
 
     void Update () {
 
         if(Input.GetButton("Fire1") && Time.time > nextFire){
 
             nextFire = Time.time + fireRate;
 
             launchPosition = cameraHeadTransform.TransformPoint(0, 0, 0.2f);
 
             pool.activate(0, launchPosition, Quaternion.Euler(cameraHeadTransform.eulerAngles.x + 90,
                                                               myTransform.eulerAngles.y, 0));
 
 
         }
     
     }

That gives me two errors:

  1. "No overload for method 'activate' takes '3' arguments" for the line where I'm trying to activate a projectile and set its transform rotation

  2. If I comment that part out to get in game, clicking the fire button gives me "Object reference not set to an instance of an object", referring to the "public GameObject activate(int id){" line from the ObjectPooler script

So my questions are, why isn't my shoot script able to reference a bullet from the object pool and how do I set the bullet's new transform after I activate it (where I used to just instantiate it)?

Comment
Add comment · Show 2
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 Kiwasi · Jun 03, 2014 at 12:36 AM 0
Share

Giving line numbers always helps to resolve errors faster.

avatar image LeftyTwoGuns · Jun 03, 2014 at 01:30 AM 0
Share

$$anonymous$$y mistake

Line 33 of my object pool script is giving the error: "Object reference not set to an instance of an object"

1 Reply

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

Answer by Kiwasi · Jun 03, 2014 at 12:35 AM

This is pretty basic programming 101. You should probably Google a few tutorials

  • On line 30 on the second set of code you call activate and give it 3 arguments.

  • On line 33 of the first set of code you define activate with 1 argument.

  • Computers are pretty amazing, but they won't work with 3=1, hence your error.

Here is my suggested fix for the errors. Starting from line 30 of the second script:

 // This gets you a reference to the activated GameObject
 GameObject current = pool.activate(0);
 // These two lines set its position and roatation
 current.transform.position = launchPosition;
 current.transform.rotation = Quaternion.Euler(cameraHeadTransform.eulerAngles.x + 90, myTransform.eulerAngles.y, 0);


I haven't checked the rest of your script for errors or looked at how well this will perform. But it will fix the errors you are getting.

The other way to solve this problem would be to give the Activate() method 2 more arguments. The best way would be to overload it with both.

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 LeftyTwoGuns · Jun 03, 2014 at 02:20 AM 0
Share

This definitely addresses the first issue. I guess my head was too far in the box to notice the bigger picture. Thanks

I'm still confused as to why I'm getting a reference error, though. I have everything plugged in the right inspectors (as far as I know) and I'm not getting any other errors in my code.

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

22 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Object pooling? 1 Answer

Creating Projectile - MoveTo/Lerp 0 Answers

I can't Implement the Pool system 1 Answer

Why my code fires bullet at random positions? 2 Answers

How can I Instantiate a prefab (projectile) consistently from the character? 0 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