enter image description here My Object Pool aka ProjectilePool prefab keeps playing when I hit play mode in Unity. It keeps shooting its projectiles without me hitting the "Fire1" mouse button.Is there some way to get it to not appear in the scene or at least to inactivate the energy ball prefab?
Here are my scripts for Object Pooling:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProjectilePooling : MonoBehaviour
{
[SerializeField]
float poolLength;
public static bool isActiveObj = false;
public GameObject projectilePrefab;
private List<Projectile> projectilesInPool;
public static ProjectilePooling Instance;
private void Awake()
{
Instance = GetComponent<ProjectilePooling>();
}
void Start()
{
PoolInitialization();
}
public Projectile Instantiate(Vector3 pos, Quaternion rot)
{
Projectile _projectile = projectilesInPool[0];
_projectile.transform.position = pos;
_projectile.transform.rotation = rot;
return _projectile;
}
public void ReturnToPooling(Projectile _projectile)
{
_projectile.transform.position = transform.position;
projectilesInPool.Add(_projectile);
}
void PoolInitialization()
{
projectilesInPool= new List<Projectile>();
for (int i = 0; i < poolLength; i++)
{
GameObject _projectile = Instantiate(projectilePrefab, transform.position, transform.rotation);
projectilesInPool.Add(_projectile.GetComponent<Projectile>());
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Projectile : MonoBehaviour
{
private Vector3 firingPoint;
[SerializeField]
private float projectileSpeed;
[SerializeField]
private float maxProjectileDistance;
private bool moves = false;
void Start()
{
firingPoint = transform.position;
}
void Update()
{
if (moves)
{
MovingTheProjectile();
}
}
public void Movement()
{
moves = true;
}
void MovingTheProjectile()
{
if (Vector3.Distance(firingPoint, transform.position) > maxProjectileDistance)
{
ProjectilePooling.Instance.ReturnToPooling(this);
}
else
{
transform.Translate(Vector3.forward * projectileSpeed * Time.deltaTime);
}
}
}
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class PlayerAttackPower : MonoBehaviour
{
[SerializeField]
Transform firingPoint;
[SerializeField]
float firingSpeed;
public static PlayerAttackPower Instance;
private float lastShootingInstance = 0;
void Awake()
{
Instance = GetComponent<PlayerAttackPower>();
}
void Update()
{
}
public void Shoot()
{
if (lastShootingInstance + firingSpeed <= Time.time)
{
Projectile _projectile = ProjectilePooling.Instance.Instantiate(firingPoint.position, firingPoint.rotation);
_projectile.Movement();
lastShootingInstance = Time.time;
}
}
}
using Mirror;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace QuickStart
{
public class PlayerScript : NetworkBehaviour
{
void Update()
{
if (!isLocalPlayer) { return; }
float moveX = Input.GetAxis("Horizontal") * Time.deltaTime * 110.0f;
float moveZ = Input.GetAxis("Vertical") * Time.deltaTime * 4f;
transform.Rotate(0, moveX, 0);
transform.Translate(0, 0, moveZ);
InputShootingManager();
}
void InputShootingManager()
{
if (Input.GetButton("Fire1"))
{
ProjectilePooling.isActiveObj = true;
//shoot attack power
PlayerAttackPower.Instance.Shoot();
}
}
}
}
It keeps my pooling object inactive and I cannot use the player's attack power. Is there a better way for me to resolve this? I greatly appreciate the help!:)