• 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 /
  • Help Room /
avatar image
0
Question by CaveGames · May 20, 2017 at 01:14 AM · onmousedown

Change Scenes by Clicking Object

Hi! My name is Jason and I am having some trouble with changing scenes. The only question im asking is how do i change scenes by clicking an object. This is being used on my title screen and i have been googling answers for about two hours now. I thought I had it with onclick but i had a slight problem! You could click the entire screen and it would change frames! I want it to only change when you click on a certain object. I have seen RayCast but have learned that it doesn't work and gives me at least 3 errors every time. If you have a fix, please give all the code, so I can learn where I was wrong and how I can improve.

Ey... Bruh... Check out... My Soundcloud... https://soundcloud.com/cjcomputers/

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
Best Answer

Answer by Jawchewa · May 20, 2017 at 02:23 AM

Raycasting is definitely one way to do it, and might be better in some situations. However, one of the easiest ways that I have found to do this is to attach a script with an OnMouseDown() function to the object that you are trying to click on. That would end up looking something like this:

     private void OnMouseDown()
     {
         SceneManager.LoadScene("Scene name");
     }

Also, you will need to make sure that the item that you are trying to click on has a colider component for this to work.

If you still want to go about it using raycasting, the solution would probably be something like this in your update function:

         if (Input.GetMouseButtonDown(0))
         {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit, 100.0f))
             {
                 //Replace this with whatever logic you want to use to validate the objects you want to click on
                 if(hit.collider.gameObject.tag == "Clickable")
                 {
                     SceneManager.LoadScene("Scene name");
                 }
             }
         }
Comment
Add comment · Show 4 · 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
avatar image CaveGames · May 20, 2017 at 12:04 PM 0
Share

Thank you for the answer, but i have a slight problem with raycast. Like I said with raycast, i always have errors with it, and no way to fix them, this is my current code

using UnityEngine; using System.Collections; using UnityEngine.Scene$$anonymous$$anagement;

public class ExampleClass : $$anonymous$$onoBehaviour { } if (Input.Get$$anonymous$$ouseButtonDown(0)) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, 100.0f)) { //Replace this with whatever logic you want to use to validate the objects you want to click on if(hit.collider.gameObject.tag == "NewGame1") { Scene$$anonymous$$anager.LoadScene("$$anonymous$$ain");

In total, there were fifteen errors with what you sent me. whenever I hover over them, it has 0 potential fixes that actually get rid of the error. If you could help me again, that would be wonderful

Signing, Jason

avatar image CaveGames CaveGames · May 20, 2017 at 12:13 PM 0
Share

I forgot to say I use C# just in case that changes anything

avatar image CaveGames CaveGames · May 20, 2017 at 01:09 PM 0
Share

Just read the part that says put it in update! thank you for the answer!

avatar image cydonia · Jul 17, 2019 at 11:03 AM 0
Share

How would you script this? like what would the full script look like?

I need a simple script that will change the scene when clicking on an object but 4 hours of looking and searching have come up with broken scrips and outdated solutions that no longer work. This might work but I can't figure out how to make this script function.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

106 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

Related Questions

when OnMouseDown() is on the right timing, it triggers an event. How to achieve it? 2 Answers

Disable mouse click on gameObject 1 Answer

why OnMouseDown is not working 0 Answers

Raycast with image over collider 0 Answers

Instantiate onMouseDown 1 Answer


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