Question about my projectiles

I just published my project boost game which is in the show forums and I’m really proud of it but… I noticed a bug that is really easily seen by everybody and it annoys me. Starting on the 2nd lvl the projectiles go crazy every time you die. They shoot several at a time and as time passes the number of projectiles at the start increases. It doesn’t do this in the unity editor at all. Any idea?

using System.Collections.Generic;
using UnityEngine;

public class Projectiless : MonoBehaviour
{
    [SerializeField] Rigidbody Projectile;
    [SerializeField] Transform barrel;
    [SerializeField] float speed = 5f;
    [SerializeField] float spawnTimer = 5.0f;
    float spawn;
    void Start()
    {
        spawn += spawnTimer;
    }

    // Update is called once per frame
    void Update()
    {
        ProjectileSpawning();
    }
    void ProjectileSpawning()
    {
        
        if (Time.time >= spawn)
        {
        Rigidbody projectileInstance;
        projectileInstance = Instantiate(Projectile, barrel.position, barrel.rotation) as Rigidbody;
        projectileInstance.AddForce(Vector3.down * speed);
        spawn += spawnTimer;
        }
    }
}```

Hi Zhain,

If you feel like the Projectiless object spawns too many projectiles at once, take another look at your code. Add Debug.Logs if in doubt. At the moment, the code looks as if there was no delay if the if-condition was true. And in that case, the code block would get executed each frame.

I tried adding a value to spawn but it didn’t change anything. I actually did try Debugging each variable but wasn’t sure exactly what I was looking for.

Okay, I debugged each variable within the if statement and it does exactly what I want it to do. I tried simulating what happens when I build the game and it wont’ do it. The game works fine within the unity editor but screws up when I’m playing the built version. I don’t get it.

Please follow Rob’s instruction in this thread. Maybe there is a helpful error message in your browser console.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms