Lasers not firing at all now

Hey everyone, this is my first time commenting here and I’m completely new to Unity and only have a little programming experience otherwise, so apologies if I mess up the formatting or something.

I wanted to use the new Input System rather than learning something that may/will become outdated, but I’m wondering if this is one of the bugs that Rick warned about because I have no idea what else it could be - although I used Debug.Log and everything appears to be correct and working, so that doesn’t really seem right either. I went back to one of my previous commits where the lasers work completely fine, and ONLY changed the code to what was changed here, even copy and pasting from the Gitlab reference. I’ve got [SerializeField] InputAction fire; and my OnEnable() and OnDisable(), my lasers set properly, etc. of course.

So this:

void ProcessFiring()
{
if (fire.ReadValue() > 0.5f)
{
ActivateLasers();
}
else
{
DeactivateLasers();
}
}

void ActivateLasers()
{
    foreach (GameObject laser in lasers)
    {
        laser.SetActive(true);
    }
}

void DeactivateLasers()
{
     foreach (GameObject laser in lasers)
    {
        laser.SetActive(false);
    }       
}

works perfectly, but this, with absolutely nothing else in my project changed:

void ProcessFiring()
{
if (fire.ReadValue() > 0.5f)
{
SetLasersActive(true);
}
else
{
SetLasersActive(false);
}
}

void SetLasersActive(bool isActive)
{
    foreach (GameObject laser in lasers)
    {
        var emissionModule = laser.GetComponent<ParticleSystem>().emission;
        emissionModule.enabled = isActive;
    }
}

does nothing. Well, not nothing. The emission module turns on and off in Play mode, and like I said Debug.Log works fine for both isActive and in ProcessFiring. I even tried compiling a build to make sure it’s not just the Unity preview being weird. Did I change some obscure setting that would make this not work or something? I’m at a total loss, and the only things I can think of to try now are changing the input system back, reinstalling Unity etc. but I want to make sure I really didn’t make some dumb mistake, which I’ve been programming just long enough to know that’s most likely what it is! I hate even asking for help, but I’ve seen how helpful you guys are here, so thank you in advance!

Hi,

Welcome to our community and good job on challenging yourself! :slight_smile:

Where exactly did you add the Debug.Logs? And did you also log Time.frameCount into your console to figure out when certain code blocks get executed?

And what was the output?

Your code looks fine to me but I’m no expert in the new Input System, so without knowing the output in your console, I cannot tell you whether this is a problem with your code, the user input or something that does not have anything to do with your code.

Hi Nina, thanks for your response! I solved the issue, but without figuring out the cause so I’ll just go through my process for the sake of anyone else potentially running into the same problem.

I didn’t know (or had completely forgotten, if it had been mentioned earlier in this course) about Time.frameCount, and can see how that’s very useful so I’m glad I posted even just to learn about that!

I’ll post where I put the Debug.Logs at the bottom, I’m not sure if there was a better way to do it (probably) but I couldn’t glean anything useful from any placements, everything seemed to look correct to me for what that’s worth. The lasers showed up just fine in scene view when I clicked play on the Particle Effects box, but when in play mode it would stay at 0 particles even when I could clearly see the emission module turned on from my input.

I manually reset the laser array and put the instances back in, checked and rechecked the prefabs and the nested prefab structure, and it was all exactly as Rick laid it out. I watched him scroll through his settings on his laser particle system and matched them, to no avail. I also tried updating Unity to the newest version, and using the old input system just to be sure while copy and pasting the needed changes from the reference code to avoid spelling/formatting errors, and exactly the same result - still no lasers. So, its probably not the new input system, right?

Finally, I just deleted my lasers including the prefab and started over from “Particle System Laser Bullet”, matching exactly what Rick did, and now it works. I also reset my settings under both Project Settings -> Player and Input Manager, but I forgot to test until a bit later so maybe I had accidentally toggled a checkbox in there or something that reset and fixed it?

I was about to give up on solving the mystery and just take the win, but then I thought to check SourceTree. I went back to the commit where I first realized the lasers were broken (and tested both, the working version before and the broken one, to make sure my labels were accurate), and I had only changed two files: PlayerControls.cs and SampleScene.Unity. It can’t have been an accidental Project Settings change, then, right?

Reversing the changed hunks in SampleScene.Unity did nothing, and then reversing PlayerControls.cs got me back to working lasers, pre-emission deactivation. So now I’m more confused than ever as to what the heck happened. Why, if only changing from Activate Lasers() and DeactivateLasers() to SetLasersActive(bool isActive) caused the lasers to break, did deleting them and starting with fresh lasers cause it to work perfectly even with the emission deactivation code? To add to my confusion, I changed back to the new input system again and copied the “broken” code into the newest commit with the working, rebuilt lasers, and it’s fine. Did one of my files get corrupted or something? Did I miss something obvious? I’m not sure, but I’m happy to have working lasers and excited to move on with the lessons!

void ProcessFiring()
{
    if (Input.GetButton("Fire1"))
    {
	//Debug.Log("Firing - Time.frameCount:" + Time.frameCount);
	SetLasersActive(true);
	//Debug.Log("firing");
    }
    else
    {
    //Debug.Log("Not firing - Time.frameCount:" + Time.frameCount);
	SetLasersActive(false);
	//Debug.Log("not firing");
    }
}

void SetLasersActive(bool isActive)
{
    foreach (GameObject laser in lasers)
    {
	var emissionModule = laser.GetComponent<ParticleSystem>().emission;
	emissionModule.enabled = isActive;
	//Debug.Log("laser:" + laser);
	//Debug.Log("lasers:" + lasers);
	//Debug.Log("emissionModule:" + emissionModule);
	//Debug.Log("isActive:" + isActive);
    //Debug.Log("end foreach loop - Time.frameCount:" + Time.frameCount);
    }
}

Good job on analysing the problem! :slight_smile:

That might well be the case, and, unfortunately, that’s not uncommon in Unity.

You wrote that you recreated the lasers. Maybe the lasers variable referenced prefabs in your Assets folder instead of the “lasers” in the Hierarchy. And when you recreated them, you assigned the correct game objects to the lasers variable. At least, that would explain why the commit where you only changed PlayerControls.cs and SampleScene.Unity seems to contain the root of the problem.

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

Privacy & Terms