Each bullet forms clone of the last
Hi, firstly I am relatively new to unity and C# in general but I am trying to create a simple 3D ball rolling platform game with a turret that shoots the player.
Currently the turret will wake up once the player is within shooting range and continue to shoot cannonballs are regular intervals. However, each cannonball forms a clone of the last one fired so it shows this in the hierarchy;
cannonballs(clone)
cannonballs(clone)(clone)
cannonballs(clone)(clone)(clone)
cannonballs(clone)(clone)(clone)(clone)
etc.
I created a cannonball destroy script to remove the cannonball clones from the game after a set amount of time and attached this script to the cannonballs prefab as seen here;
public class CannonBallDestroy : MonoBehaviour {
public float DeathTime;
void Update()
{
Destroy(gameObject, DeathTime);
}
}
However, with this script in place a new error appears in the console each time the turret tries to shoot a cannonball;
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.
As far as I understand, it appears to be destroying the prefab instead of the cannonball clones?? I am not sure what I am doing wrong here and any help would be greatly appreciated. I have tried incorporating the destroy game object into the main turretAI script and I have tried destroying the gameobject by name instead but I have had no luck and I am very confused.
Here is the main turretAI script;
public class TurretAI : MonoBehaviour {
//floats
public float distance;
public float wakeRange;
public float LastShotTime = float.MinValue;
public float fireRate;
//turretspin
public float turretSpeed;
//booleans
public bool awake = false;
//references
public GameObject Cannonballs;
public Transform target;
public Animator anim;
public GameObject BulletSpawn;
void Awake()
{
anim = gameObject.GetComponent<Animator>();
}
void Update()
{
anim.SetBool("Awake", awake);
RangeCheck();
if (awake)
{
{
//Rotate turret to look at player.
Vector3 relativePos = target.position - (transform.position);
Quaternion rotation = Quaternion.LookRotation(relativePos);
rotation.x = 0;
rotation.z = 0;
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * turretSpeed);
}
}
//fire rate
if (distance < wakeRange && Time.time > LastShotTime + (3.0f / fireRate))
{
LastShotTime = Time.time;
launchBullet();
}
}
void launchBullet()
{
Cannonballs = Instantiate(Cannonballs, BulletSpawn.transform.position, BulletSpawn.transform.rotation) as GameObject;
Cannonballs.GetComponent<Rigidbody>().AddForce(transform.forward * 500);
}
void RangeCheck()
{
distance = Vector3.Distance(transform.position, target.transform.position);
if (distance < wakeRange)
{
awake = true;
}
if (distance > wakeRange)
{
awake = false;
}
}
Thanks for reading
Your answer
Follow this Question
Related Questions
Change bullet trajectory slightly towards new player position 0 Answers
Creating a preview block for a building game[help] 0 Answers
Scripting Errors 1 Answer
Solid Object in 2D 1 Answer
no error message but script wont work 0 Answers