Input disabled after moving object in editor while in "play mode" - unity3d

This is my first question here, i couldn't find an answer online for my problem.
Here it is:
I made a simple script to print some debug info about objects when i press a key. It works as expected unless i first move any of the objects in the scene editor while the game is running.
If i move any of the objects after i hit play it seems that Input.GetKeyDown is ignored after that. I am detecting the input inside the Update function of one of the objects.
public GameObject target;
void Update ()
{
if (Input.GetKeyDown(KeyCode.P))
{
Debug.Log (transform.position);
Debug.Log (target.transform.position);
}
}
NOTE: this is not the only thing im trying to achieve with my script, but is the simplest case i could build with the same problem.
thanks in advance!

I think that's because when you move the object, the Unity GameView will lose its focus. So just ensure it has the focus again (klick into it), before hitting the key.

Related

How do you automatically focus on an inputfield when opening/activating the UI in Unity3d?

I am making a game where you must open or activate the UI with the space bar. Now, this works perfectly fine, but it is pretty annoying that every time you open the UI you must click on the inputfield to write in it. Is there any way around this? So is there a way to open or activate the UI without having to click on the field to be able to write in it?
I looked for YouTube videos and tried to find similar problems in other forums, but wasn't able to find a script, nor was I able to find some Unity settings to do so.
You could use e.g.
private class SelectOnEnable : MonoBehaviour
{
private void OnEnable()
{
EventSystem.current.SetSelectedGameObject(null);
EventSystem.current.SetSelectedGameObject(gameObject);
}
}
and attach it to whatever object that should become the selected one everytime it is enabled. See EventSystem.SetSelectedGameObject
Can't test it right now but it might still require the User to hit Enter in order to also actually set the Input field into edit mode. The upper line only sets it as selected UI element (similar to using TAB in a browser).
Otherwise I think you would go through
yourInputField.DeactivateInputField()
yourInputField.ActivateInputField();
to directly set it active. See InputField.ActivateInputField. Might have to do both in combination - again can't test right now ;)
Thank you very much, derHugo! Everything works like a charm now! You saved me a lot of time. Referring to your last comment, I used both of them, and it seems to work very well for me. Here is the code I used:
`private void OnEnable()
{
EventSystem.current.SetSelectedGameObject(gameObject);
GameManager.GetComponent().inputFieldInMainUi.ActivateInputField();
EventSystem.current.SetSelectedGameObject(null);
GameManager.GetComponent<InputFieldComparision>().inputFieldInMainUi.DeactivateInputField();
}`

After updating Unity, a second EventSystem is created on Run

I just updated from Unity 2019.3.13f1 to 2021.3.6f1 mid project. Not ideal I know, but I was advised to by Unity Support in order to add the IAP package. After the update, on running my launch scene, I get this error:
There are 2 event systems in the scene. Please ensure there is always exactly one event system in the scene
It turns out, a second EventSystem object is being created on running, which hadn't been the case before the update. At first, I just disabled the one in the hierarchy, which solved the problem as the second EventSystem then didn't conflict. However, when the game goes to a second scene and then returns to the initial scene, the duplicate EventSystem is not created, meaning that the menus don't get any input!
Can anyone advise?
This forum post (https://forum.unity.com/threads/upgrade-to-2020-lts-creating-duplicate-eventsystem.1098133/) detailed the same problem.
I have tracked this down to calling Advertisement.Initialize. If I
call that while running in the Editor; then it creates a new
EventSystem. I am not sure why. It doesn't seem to matter if I pass
true or false for the testMode value.
The poster there solved it by surrounding the Initialising line with #if !UNITY_EDITOR. However I wanted to be able to see the adverts testing in the editor - so I added this code, after Advertisement.Initialize(_gameId, _testMode, this):
var es = FindObjectsOfType<EventSystem>();
if (es.Length > 1)
{
for (int i = 1; i < es.Length; i++)
{
Destroy(es[i].gameObject);
}
}
This seems to have fixed the problem.

Best way to organize OnClick events for Unity buttons?

enter image description here
Unity has a component called Button as part of its UI system which you can use to subscribe on-click events to it through the inspector which is incredibly useful.
However, when projects get larger, I run into trouble in many situations:
events subscribed this way in the inspector are not rearrange-able which makes buttons that have lots of events difficult to manage
changing the contents of the scripts used for events can cause the button to not recognize a function that was used for an event which means you have to re-reference it
if anything happens to the GameObject or prefab that stores the Button component such as it getting corrupt then all your events that were serialized onto the button would be wiped and you would need to re-reference all of them
the above points make debugging very very difficult
What are some ways I can work around the problems I've listed above?
Inject event functions inside the code:
public Button playButton; // set button in inspector
public void Start()
{
playButton.onClick.AddListener(() =>
{
transform.position = point1;
// do something..
});
}

No function dropdown in AnimationEvent in Unity

I try to handle some events during animations, but everywhere I look, every tutorial have access to AnimatorEvent Inspector like this:
A nice simple field, where you can select a function, I want this!
But instead of this, I always getting this sick 5 fields view, and don't have any idea how to handle animation event in this case!
I tried to create function test() with debug log, but it didn't work anyway. Why I can't get access to this simple window where I can choose an function?
You will need to add this animation into a State in Animator Controller (via Animator Window).
In the object which contains Animator component, attach your script component to it.
Open the Animation window, select the object above, you will see a dropdown of animations (top left) which Animator Controller of this object contains. Choose and add event to the animation you want to.
Select the event in Animation Window, in the Inspector, you should see the dropdown of public functions of your script component attached above.
Answer by Aluminium18
Sometimes it doesn't work even if you do all things according to tutorials or advices in internet. I often leave Unity editor launched for a long time without any interactions with it. After several gibernations and several days it can get buggy - various errors appear, you can't see some functions from scripts and so on. So, just reloading the Unity editor solves many of such issues for me. And it continues to happen so for several years no matter what Unity version you have. I tried versions from 2019.x.x to 2022.x.x - all this time Unity behaves itself the same.

How to find UI in code c# (button, Panel etc)

in an empty game object i have a selection Manger script. I'm having some difficulties with finding a panel (called: OpenSelection) I created in Canvas.
I would like to find the panel where ever it is in the hierarchy and set enabled to true.
But the code isn't finding the panel. I'm not sure why.
any help would be appreciated
//UI
private GameObject panel;
// Start is called before the first frame update
void Start()
{
panel = GameObject.Find("OpenSelection");
panel.SetActive(true);
}
Generally, Find() is never the best approach for anything.
Try to set a variable reference to your OpenSelection, like you did with your panel, then call this variable.
GameObject.Find() only returns active GameObject. Here you are trying to find the OpenSelection panel which is not active. That's why the Find() is not finding the OpenSelection panel.