0

I am developing an application using Unity where I have created two Scene's.if the user gazes at an object in Scene 1 it should go to Scene 2. I have the code below, but I get errors.

SOURCE CODE:-

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class time : MonoBehaviour {

    public float gazeTime = 2f;

    private float timer;

    private bool gazedAt;


    // Use this for initialization
    void Start () {

    }
    void update(){
        if (gazedAt)
        {
            timer += Time.deltaTime;

            if (timer >= gazeTime)
            {

                Application.LoadLevel (scenetochangeto);

                timer = 0f;
            }

        }

    }
    public void ss(string scenetochangeto)
    {
        gameObject.SetActive (true);
    }

    public void pointerenter()
    {



        //Debug.Log("pointer enter");
        gazedAt = true;
    }

    public void pointerexit()
    {
        //Debug.Log("pointer exit");
        gazedAt = false;
    }
    public void pointerdown()
    {
        Debug.Log("pointer down");
    }
}
Hristo
  • 1,805
  • 12
  • 21
aish wariya
  • 21
  • 1
  • 7

2 Answers2

1

You should initialize your variables with proper values and use scene manager to load new scene as follows -

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.EventSystems;

public class time : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler {

    public float gazeTime = 2f;
    private float timer = 0f;
    private bool gazedAt = false;

    // Use this for initialization
    void Start () {

    }
    void Update(){
        if (gazedAt)
        {
            timer += Time.deltaTime;
            if (timer >= gazeTime)
            {
                SceneManager.LoadScene("OtherSceneName");
                timer = 0f;
            }
        }
    }
    public void ss(string scenetochangeto)
    {
        gameObject.SetActive (true);
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        //Debug.Log("pointer enter");
        gazedAt = true;
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        //Debug.Log("pointer exit");
        gazedAt = false;
    }
}

Change the "OtherSceneName" with name of the scene you need to load (scenetochangeto).

Mukesh Saini
  • 1,488
  • 1
  • 9
  • 16
  • yea ! thanku somuch !! i tried the same script to work with a button but its not working. how to solve it. – aish wariya May 04 '17 at 11:57
  • Add `using UnityEngine.EventSystems;` on top after `using UnityEngine.SceneManagement` – Mukesh Saini May 04 '17 at 12:54
  • I've tested it for button, image and text UI elements in canvas. Please explain the issue you are facing with it? – Mukesh Saini May 04 '17 at 12:55
  • yea! i have added it! already! actually im using gvrmain and gvr reticle. this code works for button but the issue is (" the reticle is not pointed on the button") i.e (canvas-button-text)here i have added the script to "button"->add component->add script->add new event trigger – aish wariya May 05 '17 at 06:07
  • @aishwariya your welcome! I haven't worked on gvr but ____ looks like a different issue than implementing pointer event callback methods. – Mukesh Saini May 05 '17 at 08:15
  • You can reference following resources to understand how Gaze works https://unity3d.com/learn/tutorials/topics/virtual-reality/interaction-vr , https://forum.unity3d.com/threads/how-to-use-cardboard-reticle-vr-gaze-pointer-cursor-cardboard-button-gaze-input.388492/ – Mukesh Saini May 05 '17 at 09:39
0

You didn't specified the errors you got, but pay attention: Update() is a "special" function of the Unity engine, and needs the capital U. It will never work as it is now.

Andrea
  • 6,032
  • 2
  • 28
  • 55