Hello, I just started using Unity and c#. Now I have to create a particle system which should function as a door. So it should be possible to disable or enable the Collider and the ParticleSystem in the Inspector by using a c# script. As you see below the two scripts are pretty similar, but the difference is that there's no box to tick "an" at the particle system. And anyways I'm not sure whether the code is right at all. Code (csharp): using UnityEngine; using System.Collections; public class Partikelsystem : MonoBehaviour { public bool an; void start() { an = false; } void Update () { if (an == true) { this.GetComponent<MeshCollider>().enabled = true; this.GetComponent<ParticleSystem>().Stop(); } else { this.GetComponent<MeshCollider>().enabled = false; this.GetComponent<ParticleSystem>().Play(); } }} We were told to orientate on this script, which opens/closes a simple door. Code (csharp): using UnityEngine; using System.Collections; public class OpenDoor1 : MonoBehaviour { public bool offen; void start() { offen = false; } void Update () { if (offen == true) { this.GetComponent<BoxCollider>().enabled = true; this.GetComponent<MeshRenderer>().enabled = true; } else { this.GetComponent<BoxCollider>().enabled = false; this.GetComponent<MeshRenderer>().enabled = false; } }} I'm thankful for your help!
You have added the script to a game object? an is not listed in the inspector under the script component? is the game able to run?
Heyo, It's added to a particle system! I worked on it for a while and now its looking like that: Code (csharp): using UnityEngine; using System.Collections; public class Partikelsystem : MonoBehaviour { public bool angeschalten; void Start() { angeschalten = false; } void Update () { if (angeschalten == true) { this.GetComponent<BoxCollider>().enabled = true; this.GetComponent<ParticleSystem>().enableEmission = true; } else { this.GetComponent<BoxCollider>().enabled = false; this.GetComponent<ParticleSystem>().enableEmission = false; } }} I can run the game and its listed in the inspector now, but though it doesnt work. There doesnt happen anything when I turn it on or off... Thanks for your help!
No errors! These are the screenshot, but as you can see theres no emission and no difference whether bool is on or off..
I can see that the BoxCollider is turned on and off properly. Exactly what is not working? Edit: Please show screenshots of all the settings of the particle system.
Perhaps try: Code (CSharp): using UnityEngine; using System.Collections; public class Partikelsystem : MonoBehaviour { public bool angeschalten; private BoxCollider m_collider; private ParticleSystem m_system; void Start() { angeschalten = false; m_collider = GetComponent<BoxCollider>(); m_system = GetComponent<ParticleSystem>(); } bool m_previousValue = false; void Update () { if (m_previousValue != angeschalten) { m_previousValue = angeschalten; ToggleParticle(angeschalten); } } void ToggleParticle(bool value) { m_collider.enabled = value; m_system.enableEmission = value; if (value) { m_system.Play(); } else { m_system.Stop(); } } } When running any code inside Update(), it is advisable to create a reference variable to avoid taxing CPU with constant GetComponent<T> calls. Also, having a boolean flag variable to toggle on/off prevents unnecessary calls.
After a restart of Unity it suddenly works. No idea what was going on, but I highly appreciate your help.