Hello, I am a beginner with Unity and programming in general. I am still getting the hang of OOP. I have run into an issue with the following code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LightToggle : MonoBehaviour
{
public Light targetLight;
float distance;
void Awake()
{
//player = GameObject.Find ("FPSController");
distance = 3;
}
void OnMouseOver()
{
Vector3 playerPosition = GameObject.Find("FPSController").transform.position;
if (Input.GetMouseButton(0))
{
if (Vector3.Distance(playerPosition, transform.position) <= distance)
{
targetLight.enabled = !targetLight.enabled;
Debug.Log(targetLight.enabled);
}
}
}
}
Sorry for it being a bit messy, I was up pretty late working on this. Anyway, my objective is to click a light switch and in turn (if the player is close enough to the light switch), a point light being toggled in the scene. However, when I click the light, the light inconsistently goes on or off. I used Debug.Log and found that the program was basically setting the “targetLight.enabled” property to true and false in one click. Why is this happening and how do I fix it? Thanks in advanced.