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;
}
}
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;
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?
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
}
}
this is what came to my $$anonymous$$d..I hope it help
Ahh okay thank you. Now would I set sciptEnable = true in the void Start() then use if(scriptEnable) in the trigger script?
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
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;
}
}
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
Follow this Question
Related Questions
enabling item after the other one is disabled and so on? 0 Answers
Disable light not working 0 Answers
Disable or hide GUI Buttons on one scene 2 Answers
Limiting gameObjects rotation on Zaxis giving wierd results 1 Answer
Does anyone have a good script that can be used to make a horse/vehicle rideable? 0 Answers