- Home /
UI button and raycast script that I have made both aren't working to change my scene
I am trying to make a button of some sort that switches the scene depending on if it was clicked, I have tried the method of using raycasting to tell if the button was close enough to click the button and that has gotten me pretty far, however the problem was that once I clicked it it would transfer me to the same scene even though the script wanted two different gameobjects to transfer the player to two different places, after testing this I decided to try out the ui button feature, at the moment that has gotten me basically no where since it doesn't seem to be working when I hit the button, it doesn't transfer me to the wrong scene or anything it just doesn't work, it has an event system, a canvas with graphic raycaster and everything it should need. This is my first game and I have almost no prior experience with creating games so could somebody with more experience please help me with this. Thanks in advance, below is my scripts.
My UI Button Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class ButtonPressShop : MonoBehaviour
{
public Button ShopButton;
// Start is called before the first frame update
void Awake()
{
}
// Update is called once per frame
void Update()
{
ShopButton.onClick.AddListener(Function);
}
void Function()
{
SceneManager.LoadScene("ShopMenu");
}
}
My Raycasting Script:
public Camera Cam;
public GameObject Cursor;
public Vector3 CursorPos;
public GameObject BackArrow;
public Vector3 BackArrowPos;
public bool CanClick;
// Start is called before the first frame update
void Awake()
{
Cursor = GameObject.Find("CursorTrackerForBackArrow");
BackArrow = gameObject;
}
void Update()
{
CursorPos = Cursor.transform.position;
Debug.Log(Cam.ScreenToWorldPoint(Input.mousePosition));
CursorPos = Cam.ScreenToWorldPoint(Input.mousePosition);
CursorPos.z = 0f;
Cursor.transform.position = CursorPos;
RaycastHit2D hit = Physics2D.Raycast(CursorPos, BackArrow.transform.position, 0f);
if (hit)
{
CanClick = true;
if (Input.GetMouseButton(0))
{
SceneManager.LoadScene("House");
Debug.Log("Hit");
}
}
Debug.DrawLine(CursorPos, hit.point);
BackArrowPos = BackArrow.transform.position;
}
}
My Second GameObject's raycasting script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ClickOnShop : MonoBehaviour
{
public Camera Cam;
public GameObject Cursor;
public Vector3 CursorPos;
public GameObject ShopIcon;
public Vector3 ShopIconPos;
public bool CanClick;
// Start is called before the first frame update
void Awake()
{
Cursor = GameObject.Find("CursorTrackerForIcon1");
ShopIcon = gameObject;
}
void Update()
{
RaycastHit2D Icon1hit = Physics2D.Raycast(CursorPos, ShopIcon.transform.position, 0f);
if (Icon1hit)
{
CanClick = true;
if (Input.GetMouseButton(0))
{
SceneManager.LoadScene("ShopMenu");
Debug.Log("Hit");
}
}
CursorPos = Cursor.transform.position;
Debug.Log(Cam.ScreenToWorldPoint(Input.mousePosition));
CursorPos = Cam.ScreenToWorldPoint(Input.mousePosition);
CursorPos.z = 0f;
Cursor.transform.position = CursorPos;
Debug.DrawLine(CursorPos, Icon1hit.point);
ShopIconPos = ShopIcon.transform.position;
}
}
Once again thanks to anybody who helps and sorry that you had to read all that.
Answer by guubebra · Jul 01, 2021 at 11:01 PM
Hi! I'm not sure I understood your problem completely, but I'll try to help.
First I'll talk about your UI implementation. It's a pretty simple script but has one error. The add listener function cannot be in Update function. If you do that, the function you added when clicking the button will run multiple times, and you do not want that for loading a scene. Try to do that instead:
public Button ShopButton;
// Start is called before the first frame update
void Awake()
{
ShopButton.onClick.AddListener(Function);
}
// Update is called once per frame
void Update() {}
If the error persists, I can't help you more without seeing the inspector. Just make sure all text and images in UI have target raycast off.
For your second implementation, I would replace the Input.GetMouseButton(0) to Input.GetMouseButtonDown(0), that way, the loadScene is called once.
I have not tested your second implementation because I think that the UI solution is much more elegant and simple for mouse clicking, but I hope this helps. Also, be sure that the scripts are not loading the scene at the same time by adding a Debug.Log inside the if statement of the loadScene.
I hope this helps!!