I am a beginner to scripting in Unity, and I need some help. I have this piece of code here:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class LightSwitch : MonoBehaviour {
public string myString;
public Text myText;
public float fadeTime;
public bool displayInfo;
public GameObject lightSwitch;
public AudioClip switchOn;
public AudioClip switchOff;
private AudioSource _audioSource;
void Awake ()
{
_audioSource = gameObject.GetComponentInChildren<AudioSource>();
}
// Use this for initialization
void Start () {
myText = GameObject.Find("Text").GetComponent<Text>();
myText.color = Color.clear;
}
// Update is called once per frame
void Update () {
FadeText ();
}
[B]void OnMouseOver ()
{
displayInfo = true;
if (Input.GetKeyDown(KeyCode.E)) {
lightSwitch.SetActive(false);
_audioSource.Play ();
}[/B]
}
I have more code below this but it isn’t relevant to my question. Anyways, I have two light gameobjects that are children of an Empty I called “Switch”. Since I can’t use a gameobject as a boolean, I used SetActive instead. So, my question is if there’s a way for me to press “E” to toggle my lights on and off. I am able to turn them off with the code I have now, but what is the best way to go about turning them off and on as if I had a boolean doing the work for me. Please excuse my ignorance on this matter, I’m just a beginner scripter, gotta start somewhere :-). The “OnMouseOver” code is really the main place I need help.
or “activeInHierarchy” instead of “activeSelf”- there is a difference between the two in that the former will show as inactive if the parent, grandparent, etc… is inactive. In other words, it’s “is it showing up in the scene right now”, whereas the latter will only show if the object in particular, without taking parents into account, is set to active.
I win more because I used the direct “flip” method rather than a conditional statement IMO ^_~. Neither of us used the conditional operator though, which makes me a bit sad, because it’s a fantastically horrid way to code and everyone should know about it.
Works perfectly. Didn’t realize it was that simple, lol :). Any recommendations on how to limit my light switch distance? I can literally switch it on and off across a building. Don’t give me the code, I need to learn on my own, but any hints would be great!
It would likely use a collider (a sphere collider attached to the light switch object, for instance) that the character can enter, and the OnCollisionStay function that runs every frame (like an update) that the player is inside of that collider. You can put the input code completely inside of the OnCollisionStay function, or you you can make it so that a boolean value is switched on in OnCollisionEnter and switched off in OnCollisionExit, and the bool has to be “true” to allow the input to do anything in your current script.
Just be sure to run a tag-check (check out the Collision parameter that you get automatically in those functions) when the collision events trigger, so that for instance some random object in the scene being in range isn’t setting it off every frame.
``[quote=“DonLoquacious, post:7, topic: 591138, username:DonLoquacious”]
It would likely use a collider (a sphere collider attached to the light switch object, for instance) that the character can enter, and the OnCollisionStay function that runs every frame (like an update) that the player is inside of that collider. You can put the input code completely inside of the OnCollisionStay function, or you you can make it so that a boolean value is switched on in OnCollisionEnter and switched off in OnCollisionExit, and the bool has to be “true” to allow the input to do anything in your current script.
Just be sure to run a tag-check (check out the Collision parameter that you get automatically in those functions) when the collision events trigger, so that for instance some random object in the scene being in range isn’t setting it off every frame.
[/quote]
I’ve tried using OnTriggerEnter. Here’s the code:
using UnityEngine;
using System.Collections;
public class switchDistance : MonoBehaviour {
public LightSwitch _lightSwitch;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void OnTriggerEnter (Collider col)
{
if (col.tag == “Player”)
{
Now, the issue is is that it’s opposite of what I want. It only works when I am inside of the collider (when I’m within the vicinity of where I wish to actually turn on the light). If I’m out of range it works as usual. Any ideas on how to fix this? By the way, I created a new script and attached this to my Player character gameobject.
You need to put the script on the light, not on the player, along with a collider component. Also change it so that the switch stuff happens if the col.tag IS Player, not if it ISN’T, that way only the player will be able to trigger it. You also need to make an “OnTriggerExit(Collider col)” underneath for turning it off, if your goal is that it’s always on when the player is in proximity.
You can put the entire “if(press E)” stuff from the earlier post inside of the "if(tag is player) conditional and change OnTriggerEnter to OnTriggerStay instead, if you want the player to have to hit a button to activate the light.
Ah. I am thinking my issue was because I used the actual Light Switch collider? I created an empty collider and used this script and it works perfectly now, thanks! By the way, I put the code in “else” because if the Player didn’t touch the collider then I wanted the OnMouseOver method to not work as programmed, so I hard coded how the lights performed.
using UnityEngine;
using System.Collections;
public class switchDistance : MonoBehaviour {
public LightSwitch _lightSwitch;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void OnTriggerEnter (Collider other)
{
if (other.tag == "Player")
{
_lightSwitch.OnMouseOver ();
}
else
{
_lightSwitch.displayInfo = false;
_lightSwitch.lightSwitch.SetActive(true);
_lightSwitch._audioSource.enabled = false;
}
}
}