- Home /
OnMouseDown only works once
I have a GUI application where you can click on a panel and it'll take you to a new screen, then there is another button you can press to go back to the main menu. I am using OnMouseDown
to go between the different scenes. I also have some DontDestroyOnLoad
objects that are integral to the scene. When I go to the first scene, then back to the main menu, everything appears to be fine. However, I am unable to click on that panel again. Using the update function doesn't seem very likely because I have about 8 other panels. Here is what I'm doing right now:
private void OnMouseDown()
{
if (this.gameObject.name == "Bay1")
{
Debug.Log("Bay1 Pressed");
SceneManager.LoadScene(1);
}
}
Can anyone explain to me what is happening or what I'm doing wrong and some possible fixes? Thank you!
Answer by swanne · Jul 09, 2018 at 02:49 PM
I've had a similar little issue recently which might help put you on the right track. I had multiple Panels and flicking back and forth between them caused frustrations when i previously interactable panel was no longer able to be interacted with. I found that the reason was down to additional panels blocking the raycast of the mouse click. I added a control into the functions of my scripts to modify the Sort Order of the panel and in some cases also modified the Block Raycast property. Have play with these values in the editor under play mode and see if you can click the item you wish to.
Is there any chance you still have that code you could show me as an example? And I'm not sure that is where my problem lies because my panels are definitely spaced out well enough that I don't think there would be any blocking. It feels more like the panel gets set to inactive or something when I return to the main menu. But I will definitely give this a try to see if it helps any.
I've got the code I used and will try to explain any relevance to you. $$anonymous$$y game is a multiplayer game. The main game scene has a canvas, shared by all clients. Additionally, each client has a canvas for name input.
I used the component CanvasGroup to control visibility and raycast blocking. Here's the relevant code on the player:
#region Canvas Groups
public CanvasGroup postNameSelectGroup;
public CanvasGroup playerSelect;
#endregion
public void Start()
{
if (!isLocalPlayer)
{
Debug.Log("This is not the local player");
playerSelect.alpha = 0;
return;
}
_gc = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameController>();
//Trying to get around the interupted click thing
numberOfPlayers = GameObject.FindGameObjectsWithTag("Player").Length;
_playerCanvas.sortingOrder = numberOfPlayers;
postNameSelectGroup.alpha = 0;
postNameSelectGroup.blocksRaycasts = false;
Debug.Log("PlayerScript: Yes, this is the local player");
}
void postNameInput()
{
playerSelect.alpha = 0;
playerSelect.blocksRaycasts = false;
postNameSelect.text = pname;
postNameSelectGroup.alpha = 1;
}
Hope that helps a bit
Your answer
Follow this Question
Related Questions
Unity 8.1 How to get text via script 0 Answers
Is there a reason why Unity would be able to read one variable in a script and not another? 1 Answer
After exporting my project from my PC to my laptop the physics of my game work differently. 1 Answer
Left side panel in monodevelop 1 Answer
Script problem 1 Answer