FBR Studios
FBR Studios

Reputation: 13

Changing the scene in unity using a button

I am creating a game and I need too add a button for when you die you can restart the game from a popup menu. Every time I have tried to do this the button doesn't seem to work or be pressed and the game doesn't restart. I have used the code

So this is the code to reset the scene and I have imported "using UnityEngine.SceneManagement;" to make It work

I have also added it into my button and added it to "On click"

Upvotes: 0

Views: 346

Answers (1)

Eliasar
Eliasar

Reputation: 1074

You might not have your UI canvas set up correctly.

How to detect click/touch events on UI and GameObjects

Troubleshooting the EventSystem:

No clicks detected on UI, 2D Objects (Sprite Renderer/any 2D Collider) and 3D Objects (Mesh Renderer/any 3D Collider):

A.Check that you have EventSystem. Without EventSystem it can't detect clicks at-all. If you don't have have it, create it yourself.

Go to GameObject ---> UI ---> Event System. This will create an EventSystem if it doesn't exist yet. If it already exist, Unity will just ignore it.

B.The UI component or GameObject with the UI component under be under a Canvas. It means that a Canvas must be the parent of the UI component. Without this, EventSystem will not function and clicks will not be detected.

This only applies to UI Objects. It doesn't apply to 2D (Sprite Renderer/any 2D Collider) or 3D Objects (Mesh Renderer/any 3D Collider).

C.If this is a 3D Object, PhysicsRaycaster is not attached to the camera. Make sure that PhysicsRaycaster is attached to the camera. See #6 above for more information.

D.If this is a 2D Object, Physics2DRaycaster is not attached to the camera. Make sure that Physics2DRaycaster is attached to the camera. See #7 above for more information.

E.If this is a UI object you want to detect clicks on with the interface functions such as OnBeginDrag, OnPointerClick, OnPointerEnter and other functions mentioned in #1 then the script with the detection code must be attached to that UI Object you want to detect click on.

F.Also, if this is a UI Object you want to detect clicks on, make sure that no other UI Object is in front of it. If there is another UI in front of the one you want to detect click on, it will be blocking that click.

To verify that this is not the issue, disable every object under the Canvas except the one you want to detect click on then see if clicking it works.

Upvotes: 1

Related Questions