How can i enable and disable a void function on the update function?
This is what i came up whit so far
public GameObject onOff;
// Use this for initialization
void Start()
{
onOff.SetActive(false);
}
// Update is called once per s
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
EnterNumber();
onOff.SetActive(true);
Cursor.visible = true;
}
else if (Input.GetKeyUp(KeyCode.Q))
{
onOff.SetActive(false);
Cursor.visible = false;
}
}
private void EnterNumber()
{
Debug.Log("function activated");
}
}
You don't "enable" a function. Either you call it or you don't. Your question is unclear. What do you want to do? Call EnterNumber
between the time you pressed E
and released Q
? If so:
private bool callEnterNumber;
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
{
callEnterNumber = true;
onOff.SetActive(true);
Cursor.visible = true;
}
else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Q))
{
callEnterNumber = false;
onOff.SetActive(false);
Cursor.visible = false;
}
if( callEnterNumber )
EnterNumber();
}
so what is the question? Thats seem to be ok...
if you want a toggle function, isnt necessary on this case, you can handle the semaphore from the update function
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
{
EnterNumber(true);
onOff.SetActive(true);
Cursor.visible = true;
}
else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Q))
{
EnterNumber(false);
onOff.SetActive(false);
Cursor.visible = false;
}
}
private void EnterNumber(bool Active)
{
if(active)
Debug.Log("function activated");
else
Debug.Log("function deactivated");
}
I dont know if this is what you need, please expand the question
And have a nice day for who is reading this comment!
Answer by tormentoarmagedoom · Jan 24, 2019 at 11:16 PM
Good day @DytrooydeDev
I think you need to watch some tutorial about basic scripting... A function is not "activated" A function is something that you execute, something that you can call to make things,
With this code, each frame detects if "E" has been clicked or if "Q" has been clicked.
when E is clicked, you run the EnterNumber() method, activate the gameobject onOff and make the cursor visible.
As I said, i strongly recommend you to spend 2-3 hours waching basic tutorials about Scripting and about Unity before continue. You will appreciate it.
Good luck! Bye!
You are right. Im going now to start studing. Good luck! Have a nice day!