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.
Related
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.
as the title says I have an issue loading a scene when in playmode in the editor.
The workflow of my game is as follows:
Initializing (works fine)
There is an empty scene that creates some global game objects that will exist during the entire runtime.
MainMenu (works fine)
After the Initializing is done it loads the MainMenu scene. I can interact with the scene and everything is nice.
Connect to game server (works fine)
In the main menu I have an option to connect to a game server (a dedicated server application I created on my own)
Establishing the connection and sending the login works well.
Character selection (not working)
After the login on the server I get the resonse to select a character. (This works as expected.)
Then I'm going to handle this response by opening the character selection scene.
And here I have the issue. In Playmode inside the editor the handler method is executed (verifyed by debug logs) but the scene is not loaded actualy.
When I build the game and run the created .exe and follow the exact same steps the character selection scene is loaded and shown as expected.
I searched the documentation and also the web but did not find any similar issues (maybe I still missed something)
So my question is as follows:
How do I get the scene to also load in the editor playmode? My approach seems not to be totaly wrong as it works after build.
Here is the code snippet that should load the scene:
private void MessageRecived(object sender, GNL.ResponseMessageEventArgs e)
{
GameEventMessage message = this.eventManager.MessageHandler.ParseMessage(e.Message);
Debug.Log($"Recived message with type {message.Type}");
switch (message.Type)
{
case GameEvent.CharacterSelectionRequired:
Debug.Log($"Handle character creation 01");
this.HandleCharacterSelectionRequried(message);
break;
default:
break;
}
}
private void HandleCharacterSelectionRequried(GameEventMessage eventMessage)
{
Debug.Log($"Handle character creation 02");
SceneManager.LoadScene("CharacterCreation");
}
All three Debug.Log statements are executed. Only the LoadScene isn't working in the editor playmode.
IMPORTANT ADDITION
After further testing I have to mention that the network communication is done in a seperate thread. From this thread when a new message arrives an eventHandler is called.
This is where the Method is added to the Event handler:
this.client = new GNL.GameClient(System.Net.IPAddress.Parse(host), port);
this.client.AnnounceRecivedMessage += this.MessageRecived;
this.clientNetwork = new Thread(this.client.Start);
clientNetwork.Start();
And this is the definition of the eventHandler:
public event EventHandler<ResponseMessageEventArgs> AnnounceRecivedMessage;
IMPORTANT ADDITION - Part 2
I just discovered that, it works on a normal build but not when selecting "development build" in the build settings.
This is really annoying as I have to build the game every time I made a change to test it.
I'm thankful for any help and suggestions.
After much more debugging and testing I figured out that this issue indeed is a threading issue.
Deep in the debugger I found the exception I would have expected for a threading issue. So I will have to change my implementation here.
But still there is an inconsistence in how threads are handled in Unity when running the game with debugging tools enabled (playmode in editor and development build) and with them disabled (normal build).
AFAIK, you can't switch between scenes in the editor. Unity Editor only works to edit and play the opened scene.
If you want to test some parts of the workflow, create functions dedicated to the editor (you can use #if UNITY_EDITOR) to test your scenes without the previous character selection.
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.
Currently I'm writing a script that automatically writes a file with all the scene names as variables. I would like to catch a certain event (if it exists) so that I could know when any scene was added or changed it's name. Currently I have to manually press a button whenever I add a scene or rename one. But since I'm not going to be the only one on my team using this, I'd like to automate this.
Some kind of update cycle would work too, I could then check if the current build list matches my list and if not, update it.
I've tried OnHierarchyChange / OnProjectChange, but those only seem to work on certain assets. Any idea to catch this event?
For future readers:
For Unity 5.3 and below use function OnLevelWasLoaded
http://docs.unity3d.com/530/Documentation/ScriptReference/MonoBehaviour.OnLevelWasLoaded.html
Unity 5.4 + use event: SceneManager.sceneLoaded
http://docs.unity3d.com/540/Documentation/ScriptReference/SceneManagement.SceneManager-sceneLoaded.html
I have an XNA4 wndow set up and was wondering if I could get it to accept drag+drop actions, what I envision is someone grabs a jpeg and drags it into the window, upon release of the mouse an event is fired off with a string pointing to the jpeg.
Is this doable and if so how?
First, here is a link to a tutorial on doing this with a windows form:
http://support.microsoft.com/kb/307966
and here is a link to a post about doing just this (answer is past a few posts saying that it's impossible):
http://forums.create.msdn.com/forums/p/4020/20419.aspx
finally here is some code for ease of access (you need a reference to the System.Windows.Forms namespace):
protected override void Initialize()
{
Form gameForm = (Form)Form.FromHandle(Window.Handle);
gameForm.AllowDrop = true;
gameForm.DragEnter += new DragEventHandler(gameForm_DragEnter);
gameForm.DragDrop += new DragEventHandler(gameForm_DragDrop);
}
Also, it seems it's possible to run a game inside a Form control as of XNA 2
While I recognise that this is many years too late here is a direct link to a working demo.
The SLN might not want to autoload but you can just drop it into VS2013 and it will update it. I was getting a "licencing" popup when I tried to just run the SLN.
Hope this helps anyone who might still be working on this.
http://geekswithblogs.net/mikebmcl/archive/2011/03/27/drag-and-drop-in-a-windows-xna-game.aspx