Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Switch

Discussion in 'Scripting' started by colognem, Oct 10, 2014.

  1. colognem

    colognem

    Joined:
    Oct 10, 2014
    Posts:
    6
    i want to know how to make a switch. for example, when i step on an object, a fire's particle emitter will turn on.
    the object and fire are 2 different gameobjects.
     
  2. Deecann

    Deecann

    Joined:
    Mar 17, 2010
    Posts:
    93
  3. colognem

    colognem

    Joined:
    Oct 10, 2014
    Posts:
    6
    my problem is how can i change the fire's particle emitter when the script is in the object(switch).
     
  4. Deecann

    Deecann

    Joined:
    Mar 17, 2010
    Posts:
    93
    GetComponent<ParticleEmitter>().emit
     
  5. colognem

    colognem

    Joined:
    Oct 10, 2014
    Posts:
    6
    i have many fire particles, how do the script know which to turn on or off?
     
  6. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,886
    Make the switch like this. It just says "I've been used".
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TriggerScript : MonoBehaviour {
    5.  
    6.     #region Events
    7.     public delegate void EventHandler();
    8.     public event EventHandler TriggerUsed;
    9.     #endregion
    10.  
    11.     void OnTriggerEnter(Collider collider)
    12.     {
    13.         // Sends event when trigger is used.
    14.         if (TriggerUsed != null) TriggerUsed();
    15.     }
    16.  
    17. }
    Then on your flame objects add this script. It lets you choose which triggers activate it.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class FireObjScript : MonoBehaviour {
    6.  
    7.     public ParticleEmitter flameParticle;
    8.  
    9.     public List<TriggerScript> listenToTheseTriggers;
    10.  
    11.     //============================================
    12.     // EVENTS
    13.     //============================================
    14.  
    15.     #region Event Subscriptions
    16.     void OnEnable()
    17.     {
    18.         foreach (TriggerScript triggerScript in listenToTheseTriggers)
    19.             triggerScript.TriggerUsed += OnTriggerUsed; // Run OnTriggerUsed when TriggerUsed happens
    20.     }
    21.     void OnDisable()
    22.     {
    23.         foreach (TriggerScript triggerScript in listenToTheseTriggers)
    24.             triggerScript.TriggerUsed -= OnTriggerUsed;
    25.     }
    26.     #endregion
    27.  
    28.     //============================================
    29.     // FUNCTIONS
    30.     //============================================
    31.  
    32.     void TurnOnFlame()
    33.     {
    34.         flameParticle.emit = true;
    35.     }
    36.  
    37.     //============================================
    38.     // EVENT RECEIVERS
    39.     //============================================
    40.  
    41.     void OnTriggerUsed()
    42.     {
    43.         TurnOnFlame();
    44.     }
    45.  
    46. }
     
  7. colognem

    colognem

    Joined:
    Oct 10, 2014
    Posts:
    6
    I'm sorry for asking you this but, can you translate it to javascript code?
     
  8. Deleted User

    Deleted User

    Guest

    I think you should do a a little more research into programming and not just unity.
    As unity gives you the tools you need to build a game, but you also need a basic understanding of programming.

    It does not take much to convert between JS and C#.. they are in many ways the same although C# is more strict and in my opinion a better language..

    You may be better off just learning C# if your going to code in unity.. I think most people on here use C# now days.
     
    Magiichan likes this.
  9. colognem

    colognem

    Joined:
    Oct 10, 2014
    Posts:
    6
    can you convert it to javascript? so that i would understand how it works? or at least tell me how to address other object's component?
     
  10. colognem

    colognem

    Joined:
    Oct 10, 2014
    Posts:
    6
    nevermind, i found a way. i tagged the object and then use GameObject.FindWithTag. Thanks for the effort Stardog.