Search Unity

Particle System c# problems

Discussion in 'Scripting' started by MarStanta, May 27, 2015.

  1. MarStanta

    MarStanta

    Joined:
    May 27, 2015
    Posts:
    4
    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):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class Partikelsystem : MonoBehaviour
    7. {
    8.     public bool an;
    9.    
    10.     void start()
    11.     {
    12.         an = false;
    13.     }
    14.     void Update ()
    15.     {
    16.         if (an == true)
    17.         {
    18.             this.GetComponent<MeshCollider>().enabled = true;
    19.             this.GetComponent<ParticleSystem>().Stop();
    20.         }
    21.         else
    22.         {
    23.             this.GetComponent<MeshCollider>().enabled = false;
    24.             this.GetComponent<ParticleSystem>().Play();
    25.         }
    26.     }}
    27.  
    28.  

    We were told to orientate on this script, which opens/closes a simple door.
    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class OpenDoor1 : MonoBehaviour
    7. {
    8.     public bool offen;
    9.    
    10.     void start()
    11.     {
    12.         offen = false;
    13.     }
    14.     void Update ()
    15.     {
    16.         if (offen == true)
    17.         {
    18.             this.GetComponent<BoxCollider>().enabled = true;
    19.             this.GetComponent<MeshRenderer>().enabled = true;
    20.         }
    21.         else
    22.         {
    23.             this.GetComponent<BoxCollider>().enabled = false;
    24.             this.GetComponent<MeshRenderer>().enabled = false;
    25.         }
    26.     }}
    27.  
    28.  

    I'm thankful for your help!
     
    taschtasch likes this.
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    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?
     
    MarStanta likes this.
  3. MarStanta

    MarStanta

    Joined:
    May 27, 2015
    Posts:
    4
    Heyo,
    It's added to a particle system! I worked on it for a while and now its looking like that:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Partikelsystem : MonoBehaviour
    6. {
    7.     public bool angeschalten;
    8.    
    9.     void Start()
    10.     {
    11.         angeschalten = false;
    12.     }
    13.     void Update ()
    14.     {
    15.         if (angeschalten == true)
    16.         {
    17.             this.GetComponent<BoxCollider>().enabled = true;
    18.             this.GetComponent<ParticleSystem>().enableEmission = true;
    19.  
    20.         }
    21.         else
    22.         {
    23.             this.GetComponent<BoxCollider>().enabled = false;
    24.             this.GetComponent<ParticleSystem>().enableEmission = false;
    25.  
    26.         }
    27.     }}
    28.  
    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!
     
  4. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    No errors?
    Screenshot of unity with objects selected in play mode with bool on and off please
     
    MarStanta likes this.
  5. MarStanta

    MarStanta

    Joined:
    May 27, 2015
    Posts:
    4
    No errors!
    These are the screenshot, but as you can see theres no emission and no difference whether bool is on or off.. anderes.png Scene.png
     
  6. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    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.
     
    MarStanta likes this.
  7. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    Perhaps try:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Partikelsystem : MonoBehaviour
    4. {
    5.     public bool angeschalten;
    6.  
    7.     private BoxCollider m_collider;
    8.     private ParticleSystem m_system;
    9.     void Start()
    10.     {
    11.         angeschalten = false;
    12.         m_collider = GetComponent<BoxCollider>();
    13.         m_system = GetComponent<ParticleSystem>();
    14.     }
    15.  
    16.     bool m_previousValue = false;
    17.     void Update ()
    18.     {
    19.         if (m_previousValue != angeschalten)
    20.         {
    21.              m_previousValue = angeschalten;
    22.              ToggleParticle(angeschalten);
    23.         }
    24.     }
    25.  
    26.     void ToggleParticle(bool value)
    27.     {
    28.              m_collider.enabled = value;
    29.              
    30.              m_system.enableEmission = value;
    31.              if (value)
    32.              {
    33.                   m_system.Play();
    34.              }
    35.              else
    36.              {
    37.                   m_system.Stop();
    38.              }
    39.     }
    40. }
    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.
     
  8. MarStanta

    MarStanta

    Joined:
    May 27, 2015
    Posts:
    4
    After a restart of Unity it suddenly works. No idea what was going on, but I highly appreciate your help.
     
  9. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    That's a classic one. Happy it worked out for you.