- Home /
Question by
Gergely_Maros · Nov 23, 2017 at 05:33 PM ·
inputkeysimulateother scriptkeydown
Simulate Input.GetKeyDown
Hi,
my answer is: Is it possible to trigger a key down event by code (C#)?
I have many objects with the same script on it. I have a play/pause method in this script by hitting space.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class inputController : MonoBehaviour {
private bool isPlaying = false;
void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
pause ();
}
}
void pause(){
if (!isPlaying) {
//Do something
} else {
//Do something else
}
}
}
Can I simulate a space button click to run the specific code all of my objects with the script?
Comment
Best Answer
Answer by IngeJones · Nov 23, 2017 at 05:38 PM
What I do is update a bool called Paused on my main controller object, and the update() methods of everything check for that and return if it's set (having done anything necessary like pausing a navagent journey. That saves me having to propagate events - I can just access the bool programmatically if I want to.
Dear IngeJones,
thank you for the idea of thinking differently :) It's working fine!
Cheers buddy!