- Home /
change scene on click of a 3d object
Hi All,
I'm using unity 5.3, I'm trying to change scene on click of a 3d object (cube) ( not a GUI Button).
I'm using this Javascript:
 #pragma strict
 
 function Start () {
 
 }
 
  function Update () {
          if(Input.GetMouseButton(0))
              SceneManager.LoadScene("number2");
      }
Without success, I'm having this error: BCE0005: Unknown identifier: 'SceneManager'.
I tried different code, and can't make this work . . . Any help would be amazing !
Thank you for your time
please make sure whether the name of your gameObject is cubone and not cubeone, i m saying this because Vagonn's script is correct .... plus Vagonn is not posting JavaScript code he is using C#, so copying and pasting his code in your Js file will not work, create a new c# file then paste his code 
Answer by Vagonn · Feb 09, 2016 at 07:17 PM
This will change scene all time when you click left mouse button. But you need change scene when clicking on Cube. Try this
EDIT: Create new C# script named SceneChange.cs
  using UnityEngine;
  using System.Collections;
  using UnityEngine.SceneManagement;
  
  public class SceneChange : MonoBehaviour {
  
      Ray ray;
      RaycastHit hit;
  
      void Update()
      {
          ray = Camera.main.ScreenPointToRay(Input.mousePosition);
          if (Physics.Raycast(ray, out hit) && Input.GetMouseButton(0))
          {
              if (hit.collider.name == "cubone") 
              {
                  SceneManager.LoadScene("scena2stanza");
              }
          }
      }
  }
Hello Vagonn, i've copied your java script, i've changed "your cube name" and "number2" with my name object and scene, i've put it in a cube but it doesn't work. What's wrong?
Hello. It seems I made mistake in javascript. Try this C# code.
Create new C# script named SceneChange.cs
 using UnityEngine;
 using System.Collections;
 using UnityEngine.Scene$$anonymous$$anagement;
 
 public class SceneChange : $$anonymous$$onoBehaviour {
 
     Ray ray;
     RaycastHit hit;
 
     void Update()
     {
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, out hit) && Input.Get$$anonymous$$ouseButton(0))
         {
             if (hit.collider.name == "cubone") 
             {
                 Scene$$anonymous$$anager.LoadScene("scena2stanza");
             }
         }
     }
 }
It doesn't work. I would like to say i work with gear vr and the name of my ray script is raycaster. i found it here: http://www.samsung.com/us/samsungdeveloperconnection/developer-resources/gear-vr/apps-and-games/exercise-1-creating-a-unity-project.html I put raycaster and reticle into a camera. 
sorry, with click i want say touch the touchpad.
With mouse the script works. But when i try to change scene with the ray, it doesn't work...why?
If I am correct here, which someone might really need to check, your if statement presents too much processing compared to what it could.
If I am correct the arguments in the if statement are evaluated from left to right until one is false in which case it moves on.
With this in $$anonymous$$d, if you placed your $$anonymous$$ouse-Poll first and your Raycast second, in theory, the raycast will only be performed upon the mouse co$$anonymous$$g back true.
- could be way off here and it might not work like that at all.* 
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                