Log in or register to post comments

when image is detected, trigger scene change!

May 8, 2018 - 12:18am #1

first, i'm never been using Unity program until one week ago...

-----------------------------------------------------------------

using UnityEngine.SceneManagement;

using UnityEngine;

using System.Collections;

public class BaseScript : MonoBehaviour

{

private static BaseScript instance = null;

public static BaseScript Instance

{

get

{

if (instance == null)

{

instance = FindObjectOfType(typeof(BaseScript)) as BaseScript;

if (instance == null)

{

GameObject obj = new GameObject("BaseScript");

instance = obj.AddComponent(typeof(BaseScript)) as BaseScript;

}

}

return instance;

}

}

public void SetSceneName(string _name)

{

SceneManager.LoadScene(_name);

}

void OnApplicationQuit()

{

instance = null;

}

// Use this for initialization

void Start()

{

DontDestroyOnLoad(this);

}

// Update is called once per frame

void Update()

{

}

}

i made sigleton C# file (BaseScript)

-------------------------------------------------------------------------

public void OnTrackableStateChanged(

TrackableBehaviour.Status previousStatus,

TrackableBehaviour.Status newStatus)

{

if (newStatus == TrackableBehaviour.Status.DETECTED ||

newStatus == TrackableBehaviour.Status.TRACKED ||

newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)

{

OnTrackingFound();

BaseScript.Instance.SetSceneName(Component.name);

} public void OnTrackableStateChanged(

TrackableBehaviour.Status previousStatus,

TrackableBehaviour.Status newStatus)

{

if (newStatus == TrackableBehaviour.Status.DETECTED ||

newStatus == TrackableBehaviour.Status.TRACKED ||

newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)

{

OnTrackingFound();

BaseScript.Instance.SetSceneName(Component.name);

}

and this is context on DefaultTrackableEventHandler

----------------------------------------------------------------------------------------

i wonder why compile error is occurred (An object reference is required to non access non-static member 'UnityEngine.Object.name')

and what 'Component.name' is

 

when image is detected, trigger scene change!

May 11, 2018 - 10:09am #2

Make a new script like below and add to your image target or whatever. So it overrides the default tracking behaviour.

 

This is pseudo code!

----------------------------------

using System.Collections;

using UnityEngine;

using UnityEngine.SceneManagement;

using Vuforia;



public class TargetToChangeScene : MonoBehaviour, ITrackableEventHandler

{

    #region PRIVATE_MEMBERS

    private TrackableBehaviour mTrackableBehaviour;



 

    #endregion // PRIVATE_MEMBERS

 

    #region MONOBEHAVIOUR_METHODS

    void Start()

    {

 

        mTrackableBehaviour = GetComponent<TrackableBehaviour>();

        if (mTrackableBehaviour)

        {

            mTrackableBehaviour.RegisterTrackableEventHandler(this);

        }

    }

    #endregion



    public void OnTrackableStateChanged( TrackableBehaviour.Status previousStatus, TrackableBehaviour.Status newStatus)

    {

        if (newStatus == TrackableBehaviour.Status.DETECTED ||

            newStatus == TrackableBehaviour.Status.TRACKED ||

            newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)

        {

           // Debug.Log("DETECTED!"

        SceneManager.LoadScene("<yourscenename>");

              

           

        }

        else

        {

            Debug.Log("LOST!");

        }

    }

}

Log in or register to post comments