- Home /
 
Emission blinking script
Hello,
I'm making a funfair game And I have almost everything working except the lights.
I found a script which turns on the emission after 3 seconds. But I want it to turn on and off after every 3 seconds.
Is there somebody that can help me with this?
The script I found is
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EmissionControl : MonoBehaviour
 {
 
     public Material material;
 
     void Awake()
     {
         material.DisableKeyword("_EMISSION");
     }
 
     void Update()
     {
         if (Time.time >= 3.0f)
         {
             material.EnableKeyword("_EMISSION");
         }
     }
 }
 
               Thanks in advance!
Answer by Namey5 · Mar 30, 2020 at 05:53 AM
Try this;
 private float time = 0f;
 private bool emit = false;
 
 void Update()
 {
     if (time >= 3.0f)
     {
         emit = !emit;
         if (emit)
             material.EnableKeyword("_EMISSION");
         else
             material.DisableKeyword("_EMISSION");
         time = 0f;
     }
 
     time += Time.deltaTime;
 }
 
              I tried but I got this error
Assets\EmissionControl.cs(5,15): error CS0116: A namespace cannot directly contain members such as fields or methods
Script is as follows
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 private float time = 0f;
 private bool emit = false;
 
 public class EmissionControl : $$anonymous$$onoBehaviour
 {
 
 
     public $$anonymous$$aterial material;
 
     void Awake()
     {
         material.DisableKeyword("_E$$anonymous$$ISSION");
     }
 
 }
 
     void Update()
     {
         if (time >= 3.0f)
         {
             emit = !emit;
             if (emit)
                 material.EnableKeyword("_E$$anonymous$$ISSION");
             else
                 material.DisableKeyword("_E$$anonymous$$ISSION");
             time = 0f;
         }
 
         time += Time.deltaTime;
     }
 
                 You pasted the code outside the class;
  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class EmissionControl : $$anonymous$$onoBehaviour
  {
      public $$anonymous$$aterial material;
  
      void Awake()
      {
          material.DisableKeyword("_E$$anonymous$$ISSION");
      }
  
  
      private float time = 0f;
      private bool emit = false;
      void Update()
      {
          if (time >= 3.0f)
          {
              emit = !emit;
              if (emit)
                  material.EnableKeyword("_E$$anonymous$$ISSION");
              else
                  material.DisableKeyword("_E$$anonymous$$ISSION");
              time = 0f;
          }
  
          time += Time.deltaTime;
      }
  }
 
                  Oh sorry! And what If I want to enable this script with a key press?
Lets say "E" enables it and "D" disables it.
Is thats possible?
Your answer
 
             Follow this Question
Related Questions
Emission Script 1 Answer
Trying to find the highest number than add it to itself. 2 Answers
How to code Scorpion Mechanic? 0 Answers