Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by King_of_MIlk · Jul 01, 2021 at 07:58 PM · uiraycastbuttonraycastingraycasthit2d

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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!!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

261 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a better way to find where to start my raycast? 2 Answers

How to use both NGUI widgets and unity UI 0 Answers

Player Raycast Does Not Compute 0 Answers

Why is RaycastHit2d Not working 0 Answers

UI Raycast to UI element position 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges