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 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
Add comment
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

· Add your reply
  • Sort: 
avatar image
0
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
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 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 :)

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

transform.position not adding correctly with a Vector3 1 Answer

Object doesn't move 2 Answers

How to use a Vector3.MoveTowards and the resulting position as an input for a normalised BlendTree 0 Answers

Get an Object to move to a Position for a Fixed time 2 Answers

I need help on my parented Collider2D and Vector 3 Script 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