Hi everybody!
I have FlappyBird clone type of game that is based on this tutorial:
Since completing the tutorial I have been modifying different aspects of the game. It turned into a project where pipes are giant cigarettes that emit smoke, which in turn adds to the challenge.
I would like to implement a powerup that removes the smoke effect for a set time. I have been succesful in creating a script (EmissionToggler) that on Update checks wether a boolean (smokeEnabled) is true or false and either plays or stops the emitting. I am able to access this in the inspector and can confirm that it works.
The problem arises when I try to configure the powerup to change the state of smokeEnabled in the EmissionToggler script. Unity does not give me any error messages but for some reason, the state of the boolean just does not change. As a novice coder I have no idea how to solve this, so I appreciate any help I can get.
This is the code for the powerup (the script name is terrible, I know):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PowerUpDisableSmoke : MonoBehaviour
{
private EmissionToggler emissionToggler;
private void Start()
{
emissionToggler = FindObjectOfType<EmissionToggler>();
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
emissionToggler.smokeEnabled = false;
Pickup ();
}
void Pickup ()
{
emissionToggler.smokeEnabled = true;
}
}
}
This is the code for the EmissionToggler script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EmissionToggler : MonoBehaviour
{
private ParticleSystem smoker;
//public GameObject smokeSource;
public bool smokeEnabled = true;
void Start()
{
smoker = GetComponent<ParticleSystem>();
}
private void Update()
{
if (smokeEnabled == !true)
{
smoker.Stop(true, ParticleSystemStopBehavior.StopEmitting);
}
else
{
smoker.Play();
}
}
}
In unity I basically i have a prefab called “Smokes” that has two particle systems that emit smoke (one for both the cigarettes coming from the ground and cigarettes that descend from the ceiling) as child objects. EmissionToggler is attached to both of these. The PowerUpDisableSmoke is attached to the powerup prefab.
Any help is appreciated. I am new at this but boy is it fun to create stuff with Unity!