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 *= Instantiate (Resources.Load ("projectiletest")) as GameObject;*
_ projectiles*.SetActive (false);_
_ }_
_ }*_
* private void ActivateProjectile()*
* {*
* for (int i = 0; i < numOfProjectilesToCreate; i++)*
* {*
_ if (projectiles*.activeInHierarchy == false)
{
projectiles.SetActive(true);
projectiles.GetComponent().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._
Worked perfectly. Thanks so much! I now feel i can regrow my lost hair in relative safety now :)
– Hollow_Havoc