Trailrenderers render only intermittently works / Instantiate doesn't always run

Anyone else have this issue where trail renderers sometime show and sometimes don’t? I put in a few debug statements and closely looked at the hierarchy and it seems that sometimes the objects don’t get instantiated at all. Take a look at the screen shot below.

In the code below the two debug lines get called every time but sometimes no object gets created. And if no object gets created then no arrow, no trail render, no damage, etc. Sometimes I just wait long enough while attacking one enemy and they start firing and sometimes I have to move around in the scene (still in play mode) and they start firing.

What could be the cause of Instantiate just not running? The best thing I could find while Googling is that sometimes the Instantiate operation “lags” the game engine. Maybe the frame that the Instantiate ran on ended up being a frame that was dropped? But it clearly dropped more than the visual pixel rendering. It seems to happen more when my system is stressed in terms of RAM utilization (I have 8GB - brand new system is on order).

Anyone else experience this and have a better idea of what’s going on? I’m at a loss and the above is my best hypothesis.

 public void LaunchProjectile(Transform rightHand, Transform leftHand, Health target)
        {
            Debug.Log("Pre instantiate");
            Projectile projectileInstance = Instantiate(
                projectile,
                GetTransform(rightHand, leftHand).position,
                Quaternion.identity);
            Debug.Log("Post instantiate");
            projectileInstance.SetTarget(target, weaponDamage);
        }

Take a look at the hierarchy - no arrow game object! I hit pause just after it was fired. Both debug statements ran and showed up in the console.

I found and fixed the issue. As to be expected it was my own mistake. :slight_smile:

I had a slightly different code flow inside Projectile.OnTriggerEnter to protect against a null condition. It however resulted in the projectiles colliding with the player and silently getting destroyed (without Health impact because that check was in place).

So in short the projectile objects were getting instantiated and then immediately getting destroyed.

That would do it.

My first check in OnTriggerEnter is to see of other.gameObject is the same as the owner. Good job getting that sorted!

Oh nice! So for each projectile type object you spawn you put a reference to its owner in it then?

Yes, I think we do that in the course down the line a bit (so you can get credit for shooting that poor dumb soldier who was just trying to keep his village safe from marauding adventurers).

1 Like

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

Privacy & Terms