• 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
Question by Hollow_Havoc · Dec 04, 2013 at 12:05 AM · vector3nullreferenceexceptiontransform.positionobjectpool

Problem with objectpooling and getting NullReference Exception

This is my first post so I am sorry in advanced if i have laid this out poorly or posted it in the wrong section. I just tried to be as thorough and detailed as i could be.

I'm very new to unity and making a modified bullet hell game (thought I would set myself a challenge for my first game). I've so far managed to make a character and an object pool using an array for my projectiles, after I managed (albeit i really had to try) to get my frame rate under 1FPS, and decided against using simple instantiate/destroy.

I followed along with a tutorial on youtube to make an object pool and i can get it working exactly the same as he did (albeit it is an old tutorial using setActiveRecursively) the issue i am having is spawning (activating) the projectile at my players position. Currently i can only get it to spawn a 0,0,0.

Here is the code i am using: Projectile Code;

 ***using UnityEngine;
 using System.Collections;
 
 public class projectileTest : MonoBehaviour {
 
 
     public float life =2.0f;
     public float speed = 10.0f;
     private float timeToDie = 0.0f;
     private Transform myTransform;
 
 
     void Start()
     {
         myTransform = transform;
     }
 
     // Update is called once per frame
     void Update ()
     {
         Countdown ();
         Automove ();
     }
 
     public void Activate()
     {
         myTransform.position = Vector3.zero;
         timeToDie = Time.time + life;
 
     }
 
     private void Deactivate()
     {
         this.gameObject.SetActive(false);
     }
 
     private void Countdown()
     {
         if (timeToDie < Time.time)
         {
             Deactivate();
         }
     }
 
     private void Automove ()
     {
         Vector3 velocity = speed * Time.deltaTime * Vector3.up;
         myTransform.Translate(velocity);
     }
 }***


the script for the object pool.

 ***using UnityEngine;
 using System.Collections;
 
 public class objectPool : MonoBehaviour {
 
     GameObject[] projectiles;
     public int numOfProjectilesToCreate = 0;
 
     // Use this for initialization
     void Start () {
         projectiles = new GameObject[numOfProjectilesToCreate];
         InstantiateProjectiles ();
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetMouseButtonDown(0))
         {
             ActivateProjectile ();
         }
     }
 
     private void InstantiateProjectiles()
     {
         for (int i = 0; i < numOfProjectilesToCreate; i++)
         {
             projectiles[i] = Instantiate (Resources.Load ("projectiletest")) as GameObject;
             projectiles[i].SetActive (false);
         }
     }
 
     private void ActivateProjectile()
     {
         for (int i = 0; i < numOfProjectilesToCreate; i++)
         {
             if (projectiles[i].activeInHierarchy == false)
             {
             projectiles[i].SetActive(true);
             projectiles[i].GetComponent<projectileTest>().Activate();
             return;
             }
         }
     }
 }***


I have tried swapping out the Vector3.Zero for transform.position = new Vector3(playerPosition.transform.position.x,playerPosition.transform.position.y,1); after setting a new public variable in the inspector for playerPosition

I have also tried adding the same script at the end of the activate projectile function.

Been banging my head for 3 days trying to figure this one out, and i've exhausted every logical method my limited knowledge can muster. Every time i try and change the position of activation i get a NullReferenceException on the line when i try to set the x and y axis to playerPosition.

I'm sure the answer is glaringly obvious and i'm just missing it. So I thought the best place to ask was here.

Comment

People who like this

0 Show 0
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

1 Reply

  • Sort: 
avatar image
Best Answer Wiki

Answer by T27M · Dec 04, 2013 at 12:26 AM

I'm not a 100% sure on this one, but a while back I believe I had a similar issue. Although this may be more expensive CPU wise try searching for the player instead of assigning the player transform in the inspector.

 public GameObject player;
 public void Activate()
 {
    if (player == null)
        player = GameObject.FindWithTag("Player");
 
    myTransform.position = new Vector3(player.transform.position.x,player.transform.position.y,1);
    timeToDie = Time.time + life;
  
 }

Make sure the player object is tagged in the inspector.

Comment

People who like this

0 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 Hollow_Havoc · Dec 06, 2013 at 11:59 AM 0
Share

Worked perfectly. Thanks so much! I now feel i can regrow my lost hair in relative safety now :)

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

Adding rotation to Physics.AddForce(); ... Getting weird error? 0 Answers

Spawn objects at bottom of screen / camera 2 Answers

reference an object that is yet to be in the scene 1 Answer

smoothly change position of an Object 1 Answer

Vector3 of moving object always returns (0,0,0) 1 Answer


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