- Home /
MouseButtonDown not being stored between scenes
Hi, In my game when a player presses the right mouse button in scene 0, I want scene 1 to load. (and the mouse button is kept down). When the mouse button is released in scene 1, I want scene 0 to load again.
The first switch is easy enough - but I just can't get the switch back working.
It seems that unity doesn't store which key has been pressed between scenes, so when I use
if (Input.GetMouseButtonUp(1))
in scene 1 to load scene 0 again nothing happens.
I tried attaching a script to a gameobject which isn't destroyed between scenes and wrote this
function Update () {
if (Input.GetMouseButtonDown(1))
{
Debug.Log ("mousedown");
}
if (Input.GetMouseButtonUp(1))
{
Debug.Log ("mouseup");
}
}
To see if anything would show on the console - but it isn't recognising that the mouse button is released.
Does anyone know how I would go about doing this?
Would it be possible to simulate the right mouse button down at the start of scene 1 in code so when it is released this is recognised - or is what I'm trying to do really not possible?
Best, Laurien
If Up and Down are not delivered between scenes properly then you could do your own logic with Input.Get$$anonymous$$ouseButton(0). It returns true as long as it's held down. In Scene 0, check for (the first?) true and switch scenes. In scene 1 check for false.
Hi - so I tried this out: but even though in scene 1 the right mouse button is still held down, it instantly loads scene 0 again.
I think each scene resets what keys are up and down so that they're all up - so as soon as scene 1 is loaded - it thinks the button has been released when it hasn't.
public var loaded : boolean = true;
function Update ()
{
if(loaded == true)
{
if(Input.Get$$anonymous$$ouseButton(1) == true)
{
loaded = false;
LoadDelay();
}
}
}
function LoadDelay()
{
yield WaitForSeconds(.5);
if(Input.Get$$anonymous$$ouseButton(1) == true)
{
Application.LoadLevel(1);
}
loaded = true;
}
public var loaded : boolean = true;
function Update ()
{
if(loaded == true)
{
if(Input.Get$$anonymous$$ouseButton(1) == false)
{
loaded = false;
LoadDelay();
}
}
}
function LoadDelay()
{
yield WaitForSeconds(.5);
if(Input.Get$$anonymous$$ouseButton(1) == false)
{
Application.LoadLevel(0);
}
loaded = true;
}
Answer by Yasser Jaffal · Apr 13, 2015 at 04:54 PM
I guess that the internal state of mouse button in Unity is reset on scene load, so it does not matter in which state the mouse button is when the new scene is loaded.
Answer by adder_be · Apr 14, 2015 at 03:36 PM
After some research I've concluded that unity's Input class ignores input that begun before the start of the scene. See:
http://www.reddit.com/r/Unity3D/comments/11zpj6/how_to_use_applicationloadlevel_without_resetting
http://answers.unity3d.com/questions/918300/loadlevel-resets-all-input-to-0-disastrous.html
I would suggest looking into different ways to achieve what you want. zaikman raises a valid point in his post on the reddit question above: 'Do you really need an individual scene for every room/gameview you have?'
In this particular case you could - for example - have the content of both your scene 0 and scene 1 in one combined scene, at different world positions and move the camera between these positions according to the user's mouse input. Another option would be to activated and deactivate the objects you want to display/hide.
You should try some things and use the one you feel most comfortable with.
If you still want to keep things in different scenes: other users have managed to work around the issue by using LoadLevelAdditive(), might be worth investigating that.
There are plenty of valid reasons to have each room be its own scene. It may not be the right way for everyone, but reading input between scenes should not be a request that a creator has to defend.
Your answer

Follow this Question
Related Questions
How could i execute a command move for a fighting game? 3 Answers
Best way to handle keypress? 3 Answers
Odd input glitch 2 Answers
unity3d android gui flickering rendertexture GL.Clear 0 Answers
Scoring system by pressing a series of particular keys 2 Answers