I have tried for a while now and can not get this code to work:
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.E))
{
Debug.Log("Testing");
}
}
I wanted to spawn in a GameObject (which I would have added later) when the player pressed the "E" key on their keyboard.
If you start the project from the template, the project might have enabled the new Input System. Or you might have enabled it yourself.
In any of the cases, follow these steps:
Open Preferences: Edit -> Project Settings.
Navigate to Player.
Scroll to Other Settings.
From the Dropdown Active Input Handling*, Choose Both.
According to the Information you have given, this seems to be the answer. If this doesn't solve your problem, please include some more information.
Related
I am using Unity 2020.3 and making basically a question-answer game.
Assume that I have this two functions:
public void TrueAnswer()
{
Debug.Log("True !");
}
public void FalseAnswer()
{
Debug.Log("False !");
}
In every question, true answer's button is changing(For example, in xth question the answer is in first button but in x+1th question the answer is in third button). So, I should change answer buttons onClick event in every question. Depend on my research, there is some ways like:
using UnityEngine.UI;
m_YourFirstButton.onClick.AddListener(TaskOnClick);
m_YourSecondButton.onClick.AddListener(delegate {TaskWithParameters("Hello"); });
m_YourThirdButton.onClick.AddListener(() => ButtonClicked(42));
m_YourThirdButton.onClick.AddListener(TaskOnClick);
(This is from Unity Documentation, version 2019.1)
using UnityEngine.UIElements;
button.GetComponent<Button>().clicked += ButtonClicked;
(This is what I understand from Unity Documentation version 2020.3 and what VS 2019 IntelliSense recommend me to use "clicked" event. 2020.3 also has onClicked event but it is obsolote)
First, I used the first way to set buttons onClick events and it did not work.
Then, I read the documentations and tried the second solution, which is also did not work but I get an error:
ArgumentException: GetComponent requires that the requested component 'Button' derives from MonoBehaviour or Component or is an interface.
I understand what the error is, but I am very confused. After Unity 2019.1, I can't find the UnityEngine.UI library in Unity Documentations but if I use in script I can use it without any error. So depend on the documentation I must use UnityEngine.UIElements library but if I use, how can I make buttons derive from MonoBehaviour or others?
I don't think you need to change the buttons click event at all.
Rather those can be a "static" click event that return what button index was clicked.
[0] Button 1 -> ButtonClicked(0)
[1] Button 2 -> ButtonClicked(1)
[2] Button 3 -> ButtonClicked(2)
You can then have the answers in an array
[0] Answer 1
[1] Answer 2
[2] Answer 3
and use the clicked buttons index to determine which answer was picked.
When you go to ask a new question, all you have to do is create (or replace values of) the answers array with the relevant answers (in any order you wish).
how can I enable and disable script on enemy object when I pressed specific button in unity
void OnButtonClick()
{
StartCoroutine(Cooldown());
GetComponent<FollowEnemy>().enabled = !GetComponent<FollowEnemy>().enabled;
}
GetComponent(MyScript).enabled = false;
Might be a good place to start,
For the button a Input.GetKey(KeyCode.Space), would do the trick.
You might need to experiment a bit, you can find more information here:
https://answers.unity.com/questions/26844/enabledisable-specific-components.html
Official documentation:
https://docs.unity3d.com/ScriptReference/KeyCode.html
so I am super new to unity and hololense, but I wanted to get an idea for how to add this specific feature. I want to be able to print the individual letter that is selected as the user moves through the hololens keyboard. So for example, if the user moves across the middle of the keyboard, the application should print a new line for every letter that is selected and it would look something like below.
Selected: a
Selected: s
Selected: d
....
Selected: j
Selected: k
Selected: l
I have done some research into this, but the closest thing I have found is the getkey() method, but from what I understand for that method the user needs to actually click the individual letter for it to be registered. From what I read from the Unity forum, this feature looks to be feasible, but I haven't found any specifics on how to do it. I'd really appreciate any suggestions. Thank you in advance
We recommend you use the Non-Native Keyboard of MRTK2.3 to make things easier. You just need to implement IPointerEnterHandler interface for KeyboardValueKey class in KeyboardValueKey.cs script:
public void OnPointerEnter(PointerEventData eventData)
{
Debug.Log("Select: " + Value);
}
This method is called when the pointer has hovered over a certain GameObject. Besides, if you are not familiar with NonnativeKeyboard, the example scene, in MixedRealityToolkit.Examples\Experimental\NonnativeKeyboard\Scenes\NonNativeKeyboardExample, will show how to use Non-Native Keyboard .
I have four GameObjects in the game I am making and when I click on each one a dialogue box opens.
My problem is that I am using OnMouseDown and the first one always need to be clicked twice. It happens just on the OnMouseDown, I have buttons on the game that work just fine.
public static GameObject selectedClient;
public bool isBuying;
private Manager manager;
private void Start()
{
manager = GameObject.FindObjectOfType(typeof(Manager)) as Manager;
}
private void OnMouseDown()
{
if (isBuying == true)
{
selectedClient = this.gameObject;
manager.Talk();
}
else if (isBuying == false)
{
manager.NotTalk();
}
}
The Talk() and NotTalk() are as follows:
public void Talk()
{
dialogueBox.SetActive(true);
Time.timeScale = 0f;
actualClient = Client.selectedClient;
}
public void NotTalk()
{
dialogueBox.SetActive(false);
Time.timeScale = 1f;
}
Someone knows why this happens?
Thanks in advance.
Assuming manager.Talk() opens the dialog, perhaps isBuying is false initially, and is toggled when you click once?
Edit: User updated question with Talk and NotTalk methods.
Since your say that initially, only the time stops, but the dialogueBox doesn't show, by any chance, is this because it isn't populated, or correctly initialized? Can you show is code of what your expect to show up?
It may just be that dialogueBox has nothing to display, and therefore, doesn't, however, by the second click, it somehow it's correctly populated and can display.
If I understand correctly, it seems that OnMouseDown() and Talk() is being called successfully on the first mouse click, but a second mouse click is required to get the dialogueBox to show.
If dialogueBox.SetActive(true) is being called successfully, you could take a look at the Unity Scene Hierarchy window to see if it became active like you expect after the first mouse click. If it did, then you can examine that object to see why it's not appearing on screen. (As another poster mentioned, it may be related to dialogueBox initialization.)
Also, these questions might help us debug and figure out the issue:
What error do you get when you "click on one object and then on other"?
What happens if you click the buttons in a different order? If there are 4 buttons, is it the first button that is always affected, or the first button that you click?
In general, as other posters have suggested, using Debug.Log and Debug.Assert should help you narrow down the issue. Similarly, you can set breakpoints in the code and step through to see if the code is running like you expect. And as I mentioned in this post, the Unity Scene Hierarchy window is useful for debugging as well.
Add a Debug.log(isBuying); above (isBuying == true).
Then you'll know if the OnMouseDown() is being called in the first place and if it proceeds like you expect it to. After that, you should probably be able to fix it yourself, otherwise, please let me know what you found.
I am using the treemaps for an application development. The problem I am facing is that I want to use a button to remove the level I have traversed. I have tried but there is no use. I am new to WPF programming and I am not sure about all the methods that can be used. I have put a button and I have been able to retrieve the level number and reduce the level number onClick. but the level is the same. I used drilldown property for the tree maps. But I am not sure of implementing it by use of a button. Sample is mostly preferred as a beginner. Hope for a reply sooner.
Scenario:-
I was using a squarified treemap for visualising a data that consisted of 4 levels. I drillDown the map to view certain details. For example, I have a world map that shows the continents separately. I have clicked on any of the continent, say Asia, I will get another level that shows the countries as a treemap level and further clicking on the country leads me to the cities or states. Like wise I can move backwards without an problem. Now, I am using a button to remove the levels I am currently located in. I am not able to do so and could you please help me with a code snippet for the button
I am partially there, but a detailed research will help me. It took a long time for me to settle in this.
private void Back_Click(object sender, RoutedEventArgs e)
{
int level = TreeMap.Levels.Count;
if (level >= 1)
{
TreeMap.Levels.RemoveAt(level-1);
}
}
This code can only be used to change the level, not the visual of the same level. It will not get you back to the desired screen, but you can notice the level change once you debug and use Watch.