Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by BeanSlice · Apr 27, 2017 at 05:55 PM · c#scripting problemdisableflashlight

Trouble disabling and enabling script via another script

So basically, I am trying to create a trigger script that when entered will turn the players flashlight off, then after 4 seconds, turn it back on again. It also needs to be able to disable the players flashlight script so they cannot enable the flashlight manually before these 4 seconds have passed. I'm not sure how to reference other scripts via script, so I'm not too sure what I need to do to get this right, but here is my code so far (everything works as intended but this). The flashlight script that I am attempting to reference is called flashlight:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TriggerScriptFlashlightKill : MonoBehaviour 
 {
 
     public AudioClip scareSound;
 
     public GameObject triggerZone;
 
     public Light lightSource;
 
     public GameObject script;
 
     private bool hasPlayed = false;
 
     AudioSource scareAudio;
 
     private flashlight flashlightScript;
 
     void Start ()
     {
         scareAudio = GetComponent<AudioSource> ();
         triggerZone = GetComponent<GameObject> ();
         flashlightScript = script.GetComponent("flashlight") as flashlight;
     }
 
     void Update ()
     {
 
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag ("Player")) 
         {    
             if (!hasPlayed) 
             {
                 scareAudio.PlayOneShot (scareSound);
                 hasPlayed = true;
                 StartCoroutine(FlashlightFlicker());
                 Destroy (triggerZone.gameObject);
             }
         }
     }
     IEnumerator FlashlightFlicker()
     {
         flashlightScript.enabled = false;
         lightSource.enabled = false;
 
         yield return new WaitForSeconds (4);
 
         flashlightScript.enabled = true;
         lightSource.enabled = true;
 
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by anil4029 · Apr 28, 2017 at 07:41 PM

Try this :

 using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class TriggerScriptFlashlightKill : MonoBehaviour 
  {
  
      public static TriggerScriptFlashlightKill instance {get; set;}
      public AudioClip scareSound;
  
      public GameObject triggerZone;
  
      public Light lightSource;
  
      public GameObject script;
  
      private bool hasPlayed = false;
  
      AudioSource scareAudio;
  
      private flashlight flashlightScript;
  
      void Start ()
      {
          instance=this;
          scareAudio = GetComponent<AudioSource> ();
          triggerZone = GetComponent<GameObject> ();
          flashlightScript = script.GetComponent("flashlight") as flashlight;
      }
  
      void Update ()
      {
  
      }
  
      void OnTriggerEnter(Collider other)
      {
          if (other.gameObject.CompareTag ("Player")) 
          {    
              if (!hasPlayed) 
              {
                  scareAudio.PlayOneShot (scareSound);
                  hasPlayed = true;
                  StartCoroutine(FlashlightFlicker());
                  Destroy (triggerZone.gameObject);
              }
          }
      }
      IEnumerator FlashlightFlicker()
      {
          flashlightScript.enabled = false;
          lightSource.enabled = false;
  
          yield return new WaitForSeconds (4);
  
          flashlightScript.enabled = true;
          lightSource.enabled = true;
  
      }
  }



Now use this instance to acess any public method or variable from this class eg. TriggerScriptFlashlightKill.instance.scareSound;

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BeanSlice · Apr 28, 2017 at 07:49 PM 0
Share

Thank you! (Just wondering - not intending to be rude but more to clear things up) Why would I need to reference a variable in the flashlight script which is from the trigger script when the trigger script uses the light which the flashlight script already uses?

avatar image anil4029 · Apr 28, 2017 at 07:57 PM 0
Share

you want to disable and enable a script...just make a bool variable in that script and use it to switch..

 void Update(){
 
 if(scriptEnable){
 // code to run
 }
 
 }
avatar image anil4029 anil4029 · Apr 28, 2017 at 07:58 PM 0
Share

this is what came to my $$anonymous$$d..I hope it help

avatar image BeanSlice anil4029 · Apr 28, 2017 at 08:00 PM 0
Share

Ahh okay thank you. Now would I set sciptEnable = true in the void Start() then use if(scriptEnable) in the trigger script?

avatar image anil4029 BeanSlice · Apr 28, 2017 at 08:05 PM 0
Share
      IEnumerator FlashlightFlicker()
      {
 
          flashlightScript.instance.scriptEnable= false;
          lightSource.enabled = false;
  
          yield return new WaitForSeconds (4);
  
           flashlightScript.instance.scriptEnable= true;
          lightSource.enabled = true;
  
      }

creat instance in flashlightScript

Show more comments
avatar image
0

Answer by BeanSlice · Apr 28, 2017 at 08:18 PM

Okay... thank you both for being so patient! I finally got it to work (somehow)! Thank you both again :) I'll show you both scripts: Flashlight:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI; //this allows us to reference anything to do with the UI in our script. *MUST BE ADDED IN ORDER TO USE UI IN SCRIPTS*
 
 public class flashlight : MonoBehaviour 
 {
 
     public Light lightSource; //allows us to add a light source to be toggled.
     public AudioClip soundOn; //allows us to add a sound to be played everytime the flashlight is toggled.
     public AudioClip soundOff;
     public float maxBatteryLife;
     public float drainSpeed;
     public KeyCode key; //this will allow us to choose our desired key to toggle the flashlight outside of the script.
     public Text isontext; //this is used as a reference to the text that will be updated by our script.
     private bool isOn = false; //this will make the text display the light as being off at the beginning of the game, rather than displaying nothing.
     AudioSource lightaudio;
     public bool scriptEnable = true;
 
     void Awake () //awake is used to initiate game states and variables before the game starts - this is only ran once during the script's lifetime. We only need to grab the audio source once, so we used awake to do so before anything else can happen.
     {
         lightaudio = GetComponent<AudioSource> (); //grabs the audio source component - allows sound to be played.
     }
 
     void Start () //Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.
     {
         lightSource.enabled = false; //this will start the game with the flashlight off.
         //displaytext();
     }
 
     void Update () //anything within the update function will be called/updated every frame.
     {
         if (scriptEnable == true) 
         {
             if (Input.GetKeyDown (key)) 
             { //if the specified key (set in the Inspector) is pressed, then do the following
                 isOn = !isOn; //the boolean isOn will be toggled when the specified key is pressed.
                 //displaytext();
                 playsound ();
             }
             if (isOn) 
             {
                 lightSource.enabled = true; //the "!" operator means not, you can toggle something by setting it to the opposite value of itself, which is what we are doing here with the light source. You can toggle a light source by toggling its enabled state.
 
             } else 
             {
                 lightSource.enabled = false;
             }
         } else 
         {    
             scriptEnable = false;
         }
     }
     void displaytext () //using the same line of code more than once in inefficient, therefore I made the line of code a function, so instead of using that same line of code, I can call the function. This uses less memory.
     {
         isontext.text = "Light: " + isOn.ToString (); //the boolean variable isOn will be displayed on the screen whether it is true or false (testing text).
     }
     void playsound () //this function allows us to play a different sound when the flashlight is toggled on or off.
     {
         if (lightSource.enabled == true) //if the flashlight is on, then play the sound indicating that the flashlight has been toggled on.
             lightaudio.PlayOneShot (soundOn);
         if (lightSource.enabled == false) //if the flashlight is off, then play the sound indicating that the flashlight has been toggled off.
             lightaudio.PlayOneShot (soundOff);
     }
 }

TriggerScriptFlashlightKill:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TriggerScriptFlashlightKill : MonoBehaviour 
 {
 
     public static TriggerScriptFlashlightKill instance {get; set;}
 
     public AudioClip scareSound;
 
     public GameObject triggerZone;
 
     public Light lightSource;
 
     public GameObject script;
 
     private bool hasPlayed = false;
 
     AudioSource scareAudio;
 
     private flashlight flashlightScript;
 
     void Start ()
     {
         instance = this;
         scareAudio = GetComponent<AudioSource> ();
         triggerZone = GetComponent<GameObject> ();
         flashlightScript = script.GetComponent<flashlight>();
     }
 
     void Update ()
     {
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag ("Player")) 
         {    
             if (!hasPlayed) 
             {
                 scareAudio.PlayOneShot (scareSound);
                 hasPlayed = true;
                 StartCoroutine(FlashlightFlicker());
                 Destroy (triggerZone.gameObject);
             }
         }
     }
     IEnumerator FlashlightFlicker()
     {
         flashlightScript.scriptEnable = false;
         lightSource.enabled = false;
 
         yield return new WaitForSeconds (4);
 
         flashlightScript.scriptEnable = true;
         lightSource.enabled = true;
     }
         
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ruri-Dev · Apr 28, 2017 at 08:50 PM 0
Share

Cool, just don't forget to give a thumbs up if something was useful, of course, don't do it if it wasn't.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

355 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Disable light not working 0 Answers

Disable or hide GUI Buttons on one scene 2 Answers

enabling item after the other one is disabled and so on? 0 Answers

No input coming from using new input system, and variable not viewed in inspector. 0 Answers

Vector3.Lerp in update, how stop? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges