In the code below I don’t get this part:
public void OnMouseEnter()
{
if (firstclick == true && active == true)
{
Clicked();
}
}
It says if firstclick
and active both are true
then execute the Clicked()
method. And then when Clicked()
method is executed active turn to false
. BUT how come this code works?
It should not be work. Could someone explain me?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ClickControl : MonoBehaviour
{
public static string entered = "";
public static bool firstclick = false;
public bool active = true;
TextMesh text;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(firstclick == false)
{
text = GetComponent<TextMesh>();
text.color = Color.white;
active = true;
}
}
public void Clicked()
{
entered += GetComponent<TextMesh>().text;
text = GetComponent<TextMesh>();
text.color = Color.yellow;
Debug.Log(entered);
active = false;
}
public void OnMouseDown()
{
if(firstclick == false)
{
Clicked();
firstclick = true;
}
}
public void OnMouseEnter()
{
if (firstclick == true && active == true)
{
Clicked();
}
}
public void OnMouseUp()
{
firstclick = false;
entered = "";
}
}
EDIT:
So in that code, there’s this OnMouseEnter()
function that checks if both firstclick
and active are true
, and if they are, it calls the Clicked()
function, right? But the thing is, when Clicked()
runs, it changes active to false, so logically, you’d think it won’t ever trigger again because active becomes false
.