Log in or register to post comments

OnTrackingLost() and OnTrackingFound()...

December 31, 2010 - 11:44am #1

Ahoy!

Quick question, and it may be a result of my poor coding skills...

I'd like to have some sort of message appear when the tracker is lost off screen/ camera. I see i need to use the OnTrackingLost() and OnTrackingFound() functions in the TrackableScript.cs file, but i'm not sure exactly how to access that function from a javascript file. With other .cs plugins i've been able to just call the function just like regular unity functions. Didn't quite work in my case.

Any thoughts to best handle this?

Avast and thank ye,
CapnJ

OnTrackingLost() and OnTrackingFound()...

August 18, 2015 - 10:45am #51

Hi! I just want to ask on how can the objects can be seen even if I remove it in the marker.

OnTrackingLost() and OnTrackingFound()...

July 2, 2013 - 2:56am #50

Hi sonal123,

if you are using Vuforia for iOS (and you are not using Unity), this is the wrong thread. It would be better to post your question in the iOS category:

https://developer.vuforia.com/forum/ar-technical-discussion/ios

 

OnTrackingLost() and OnTrackingFound()...

July 2, 2013 - 2:34am #49

Hi,

I am using vuforia for ios, wanted to know the event  which is called when tracking is lost...

Please help

OnTrackingLost() and OnTrackingFound()...

May 26, 2013 - 7:45pm #48

Thanks , I have added the Application.open Url in the ontracking method, but when I want to rebuild again after ing this urladdrtting this it showing error Default parametr specified are not permitted--- Please cna u able to inform about the steps are required adding this url

OnTrackingLost() and OnTrackingFound()...

May 26, 2013 - 7:11pm #47

Thanks I am trying to do this.......

OnTrackingLost() and OnTrackingFound()...

May 26, 2013 - 5:55pm #46

Are you trying to load and play the video, or open a page containing the video?

If it's the latter, you can use Application.OpenURL within the DefaultTrackableEventHandler's OnTrackingFound method to call a browser instance and load the YouTube page - http://docs.unity3d.com/Documentation/ScriptReference/Application.OpenURL.html

But if you want to load the video into your app, you'll need to familiarize yourself w/ Vuforia's Video Playback capabilities - https://developer.vuforia.com/resources/sample-apps/video-playback-sample-app

OnTrackingLost() and OnTrackingFound()...

May 26, 2013 - 5:27pm #45

Hello Kim,

                     I want to open a  youtube link when my Image Target will be found by  ARcamera in Android Applications, Please can you help me in this regard, its very urgent, I am trying for a week, but I am unable to do this.

Re: OnTrackingLost() and OnTrackingFound()...

July 5, 2011 - 11:48am #44

Hello

The TrackableEventHandler.cs script should be on your ImageTarget and not the ARCamera. Take a look at the ImageTargets sample app for an example of how to use the TrackableEventHandler.cs script.

Thank you,

-Peter

Re: OnTrackingLost() and OnTrackingFound()...

July 2, 2011 - 3:56pm #43
ksiva wrote:

You now need to implement the ITrackableEventHandler interface to capture these events. In the ImageTargets sample, the Scripts/TrackableEventHandler.cs script contains the OnTrackingLost/OnTrackingFound methods.

- Kim

I've tried to use the TrackableEventHandler.cs in my project but I have a problem. The script is on my ARCamera and I've added a GUIText to display some messages during testing:

 void Start()
    {
		
		txt.text="start";
        mTrackableBehaviour = GetComponent<TrackableBehaviour>();
        if (mTrackableBehaviour)
        {
			
            mTrackableBehaviour.RegisterTrackableEventHandler(this);
			txt.text="done";
        } else {
			txt.text="error";
		}
		
    }

It will end up in the error block, so mTrackableBehaviour is empty.
How can I fix that?

Re: OnTrackingLost() and OnTrackingFound()...

June 15, 2011 - 12:17pm #42

Working in unity with the Qcar-Beta2 version, objects would always be bound to a target if they were placed inside the ImageTarget in the hierarchy. Why does this not work the same in 1.0? I upgraded a project to 1.0 and when I move off target, my object/animation goes with the camera. but will re-track if back on target.
Is this relevant?

Re: OnTrackingLost() and OnTrackingFound()...

June 9, 2011 - 6:06am #41

You now need to implement the ITrackableEventHandler interface to capture these events. In the ImageTargets sample, the Scripts/TrackableEventHandler.cs script contains the OnTrackingLost/OnTrackingFound methods.

- Kim

Re: OnTrackingLost() and OnTrackingFound()...

June 8, 2011 - 12:55am #40

Hi,

I have a problem with the Unity 3 Plugin Extension.
I managed to run the ImageTarget sample, it's work fine and I try, now, to add my own devlopment on a tracking detection. Unfortunately, I don't find the TrackableScript.cs and the functions OnTrackingLost() and OnTrackingFound().

I use the QCAR Unity 3 Extension 1.0.0 and Unity 3.3.0f4.

Anybody can help me ?

Thanks,

Re: OnTrackingLost() and OnTrackingFound()...

March 23, 2011 - 6:53am #39

Thankx.its work fine .The only thing is i attached MyButton script to the gameobject.

Re: OnTrackingLost() and OnTrackingFound()...

March 22, 2011 - 8:18pm #38

If you are still using this line of code to get the button:

MyButton mb = gameObject.GetComponent<MyButton>();

be aware that it will only find a MyButton script attached to the current marker. You may want to create a globally shared button and find it by name.

- Kim

Re: OnTrackingLost() and OnTrackingFound()...

March 22, 2011 - 7:12am #37

Hi kim,
I am able to enable and disable button onTrackinLost() and onTrackingFound() with one armarker as posted previously.But I want to enable and disable the button on multiple armarker.but problem is that when i move from one ar marker to another marker the button is not disabled.It is sticking onto the screen.What may be the problrm?

Re: OnTrackingLost() and OnTrackingFound()...

March 20, 2011 - 6:19pm #36

I'm not sure I completely understand, but it sounds like you want to do some math based on the camera's position/orientation relative to the target. Unity has a lot of handy math functions for this sort of thing, for instance look at the Transform.InverseTransformDirection method for taking a vector from one object into another object's coordinate system. You could use this to say take the camera's forward vector into target space, to figure out which direction the camera is looking in relation to the target. I'll leave the math up to you, but Unity should provide everything you need!

- Kim

Re: OnTrackingLost() and OnTrackingFound()...

March 19, 2011 - 10:41am #35

Hi kim,
I want play animation of animation in which direction i moved the armarker on OnTrackingLost().I am able to play animation in only one direction by reading the above threads.But when i move myarmarker e.g i turn towards right ,then my 3d model should move towards right.If i turn towards left it the 3dmodel model move left.how is it Possible?

Re: OnTrackingLost() and OnTrackingFound()...

March 19, 2011 - 3:26am #34

thankx kim video atlast playing.this above error i had solved
I go to androidmanifestfile.
I add like

it works fine.

Re: OnTrackingLost() and OnTrackingFound()...

March 18, 2011 - 11:32pm #33

Hi kim,
i followed alternative way to play the video. As per you,OnTrackingLost() I wrote as

haveSeenTrackable=true;
      if (haveSeenTrackable) {
       MyScript ms = gameObject.AddComponent<MyScript>();
	  
	  }

in MyScript i wote as

 
function OnGUI () {
	
	if (GUI.Button (Rect (20,5,200,25), "ControlMode.Full")) {
	    //iPhoneSettings.screenOrientation = iPhoneScreenOrientation.LandscapeLeft;
       	iPhoneUtils.PlayMovie("intro.mp4", Color.yellow, iPhoneMovieControlMode.Full);	
		Debug.Log("Movie-ing");
	}
	
	if (GUI.Button (Rect (20,55,200,25), "ControlMode.VolumeOnly")) {
	
       iPhoneUtils.PlayMovie("intro.mp4", Color.green, iPhoneMovieControlMode.Minimal);	
		Debug.Log("Movie-ing");
	//}

	if (GUI.Button (Rect (20,95,200,50), "ControlMode.CancelOnTouch")) {
       	iPhoneUtils.PlayMovie("intro.mp4", Color.blue, iPhoneMovieControlMode.CancelOnTouch);	
		Debug.Log("Movie-ing");
	}
	
	if (GUI.Button (Rect (20,155,200,25), "ControlMode.Hidden")) {
       	iPhoneUtils.PlayMovie("intro.mp4", Color.red, iPhoneMovieControlMode.Hidden);	
		Debug.Log("Movie-ing");
	}
///////////////////////////////////
}

Also i put the video in streaming assests.
OntrackingLost()
I am able to display above 4 buttons.When i press CancelOnTouch for playing
its give the error as
03-19 11:53:31.635: ERROR/AndroidRuntime(6488): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.milan.MyProj/com.unity3d.player.VideoPlayer}; have you declared this activity in your AndroidManifest.xml?
What may br ploblem?
Its playing video in normal unity3d project.

Re: OnTrackingLost() and OnTrackingFound()...

March 18, 2011 - 10:06am #32

yaa kim its working in unity3d.But not working when onTrackingLost(0;I have attached this script.its working on normal unity3d.The mpe4 video is placed in Steamingassest.
#pragma strict
#pragma implicit
#pragma downcast

function Start()
{
// make portrait view
iPhoneSettings.screenOrientation = iPhoneScreenOrientation.LandscapeLeft;
}

private var selectedVideo = "MyVideo.mp4";

function OnGUI () {

if (GUI.Button (Rect (20,5,200,25), "ControlMode.Full")) {
iPhoneUtils.PlayMovie(selectedVideo, Color.yellow, iPhoneMovieControlMode.Full);
//Debug.Log("Movie-ing");
}

if (GUI.Button (Rect (20,55,200,25), "ControlMode.VolumeOnly")) {
iPhoneUtils.PlayMovie(selectedVideo, Color.green, iPhoneMovieControlMode.Minimal);
//Debug.Log("Movie-ing");
}

if (GUI.Button (Rect (20,95,200,50), "ControlMode.CancelOnTouch")) {
iPhoneUtils.PlayMovie(selectedVideo, Color.blue, iPhoneMovieControlMode.CancelOnTouch);
//Debug.Log("Movie-ing");
}

if (GUI.Button (Rect (20,155,200,25), "ControlMode.Hidden")) {
iPhoneUtils.PlayMovie(selectedVideo, Color.red, iPhoneMovieControlMode.Hidden);
//Debug.Log("Movie-ing");
}
///////////////////////////////////
}
so i put the above in android plugin .then ontracking Lost()
i write as
if (haveSeenTrackable) {
MyScript mb = gameObject.GetComponent();
if (mb)
mb.enabled = true;
}
but its not working what maybe the problem?

Re: OnTrackingLost() and OnTrackingFound()...

March 18, 2011 - 7:39am #31

I'm not sure how to do this in Unity, but I'm sure there's a way... Try to figure out how to play a video first, if you can get that working in a non-QCAR project then it should be pretty simple to make it happen when tracking is lost.

I think the iPhoneUtils.PlayMovie method works on Android as well:
http://unity3d.com/support/documentation/ScriptReference/iPhoneUtils.PlayMovie.html

- Kim

Re: OnTrackingLost() and OnTrackingFound()...

March 18, 2011 - 7:25am #30

Hii kim,
thankx.But i change my mind.
i want to play a video when ar tracker is lost?how this possible?

Re: OnTrackingLost() and OnTrackingFound()...

March 18, 2011 - 7:14am #29

Please surround your code snippets in

 tags, it's really hard to read otherwise.  There's a little button on the formatting bar for this, a # sign.

If you really want to create and destroy the MyButton script each time, you could try something like this:

[CODE]
bool haveSeenTrackable = false;

public void OnTrackingFound()
{
    ...
    
    haveSeenTrackable = true;
    
    MyButton mb = gameObject.GetComponent<MyButton>();
    if (mb)
        Destroy(mb);
}

public void OnTrackingLost()
{
    ...
    
    if (haveSeenTrackable) {
        gameObject.AddComponent<MyButton>();
    }
}

It might be better to attach the MyButton script to the trackable, and enable/disable it rather than creating and destroying it each time:

bool haveSeenTrackable = false;

public void OnTrackingFound()
{
    ...
    
    haveSeenTrackable = true;
    
    MyButton mb = gameObject.GetComponent<MyButton>();
    if (mb)
        mb.enabled = false;
}

public void OnTrackingLost()
{
    ...
    
    if (haveSeenTrackable) {
        MyButton mb = gameObject.GetComponent<MyButton>();
        if (mb)
            mb.enabled = true;
    }
}

Make sure you disable the script when you attach it to the trackable (uncheck it) so it starts hidden.

Finally, the MyButton.js file should be really simple:

function OnGUI () {
    if (GUI.Button (Rect (20,40,80,20), "ViewMyCar")) {
        Application.LoadLevel (1);
    }
}

- Kim

Re: OnTrackingLost() and OnTrackingFound()...

March 18, 2011 - 4:57am #28

Hi kim,

I am working with two scenes.In the first scene there is a ar marker.What I want to do is,when tracking is lost,I want a button to appear.Onclicking this, I want to go to next scene. Then while navigating from second scene to first, I do not want the button to appear. That is,I want the button to appear onTrackingLost() and disable it onTrackingFound(). I am using 2 flags for this. Initially I am setting both to zero value.
OnTrackingFound() i am setting First flag to one.OnTrackingLost() i write
public void OnTrackingLost()
{
Renderer[] rendererComponents = GetComponentsInChildren();

foreach (Renderer component in rendererComponents) {
component.enabled = false;
}
if((flag==1)&&(seenCar==0)){//flag and seenCar are my two flags
print("i call my flag OnTrackingLost");
seenCar=1;
MyButton mb=new MyButton();
mb.ButtonEnable();
print("button is enabled");

}
/*if((flag==1)&&(seencar==1)){
seenCar=0;
MyButton mb=new MyButton();
mb.Update();
//Application.LoadLevel (1);}

}*/
}
in javascript I wrote like this
function ButtonEnable () {
OnGUI();
print("calling in the in buttonenable()");
//Application.LoadLevel ("MyButton");
}
function OnGUI()
{
if (GUI.Button (Rect (20,40,80,20), "ViewMyCar")) {

Application.LoadLevel (1);
print("after loadlevel");

}}
OnTrackingLost() the button is not appearing.what maybe the problem??

Re: OnTrackingLost() and OnTrackingFound()...

March 7, 2011 - 7:31am #27

Can you possibly send the script to

? It's difficult for me to debug with just the information here. You should be able to reset the position of the object by resetting the localPosition portion of the transform, but you may also need to stop/reset the animations.

- Kim

Re: OnTrackingLost() and OnTrackingFound()...

March 7, 2011 - 6:25am #26

Hello Kim,
I placed a gameobject(a mouse) on the image target at a particular position,when the page is turned from left to right i want it(gameobject) to walk from left to right,and similarly when it is turned from right to left i want it to walk from right to left and when the tracking is found it should be at the position where it was initially,
I tried doing this with the code you gave before but i am not able to fix the position of the gameobject back to the original position.

Re: OnTrackingLost() and OnTrackingFound()...

March 3, 2011 - 5:39am #25

Perhaps you just need to reset the starting position of the model. If you are still animating on localPosition.x, you could try adding something like this before animating (or when the target is found):

myAnimation.transform.localPosition = new Vector3(0, myAnimation.transform.localPosition.y, myAnimation.transform.localPosition.z);

- Kim

Re: OnTrackingLost() and OnTrackingFound()...

March 3, 2011 - 2:38am #24

hiii kim im back with a small issue,when i play the animation sometimes the object starts from the middle of the screen and drives off this is what i want,but sometimes it is starting from somewhere else and it is coming onto the middle of the screen and stopping,any solution for this it should always start from the middle of the screen???

Re: OnTrackingLost() and OnTrackingFound()...

February 28, 2011 - 12:36am #23

Thank you very much you helped me a lot,thank you once again..Will post again if i have any doubts..

Re: OnTrackingLost() and OnTrackingFound()...

February 25, 2011 - 7:05am #22

I think the easiest way might be to move the camera in relation to the model. The camera will automatically be moved back when tracking is found again, so you don't have to do any cleanup work this way. You could do something like this in OnTrackingLost:

Camera.main.transform.position = myModel.transform.position + myModel.transform.forward * 500;
Camera.main.transform.LookAt(myModel.transform);

Of course you'll have to play around with the position and distance, but that's the general idea. You may want to use myModel.transform.right or myModel.transform.up instead, for example.

- Kim

Re: OnTrackingLost() and OnTrackingFound()...

February 25, 2011 - 6:23am #21

Yaa i did that before itself,what you said is absolutely correct the model will be stuck in the last place the target was found.Sometimes it is visible on the screen and sometimes it is going invisible may be it is getting stuck somewhere else,can u tell me how to make it look that it is on centre of the screen??
U also said that "You could move the model to a better position in the OnTrackingLost callback if you wanted"
can u explain me how to do it??

Re: OnTrackingLost() and OnTrackingFound()...

February 25, 2011 - 6:03am #20

You can comment out these lines in TrackerScript.cs:

if (m_PoseType == PoseType.Camera)
    camera.transform.localPosition = m_CamOutOfScenePosition;

Now the camera should never jump away. Note that once tracking is lost, the model will be stuck in the last place the target was found, which might not be squarely in front of the camera. You could move the model to a better position in the OnTrackingLost callback if you wanted, just be sure to move it back in OnTrackingFound.

- Kim

Re: OnTrackingLost() and OnTrackingFound()...

February 25, 2011 - 3:53am #19

Yaa now i am also able to play animation when tracking is lost,but the camera settings are like Position camera far away from scene if targets are not detected so thats y i cannot see the animation properly.Any solution for this??

Re: OnTrackingLost() and OnTrackingFound()...

February 24, 2011 - 9:39am #18

First of all you will need to remove this section from the OnTrackingLost method:

/* comment this out
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();
foreach (Renderer component in rendererComponents) {
    component.enabled = false;
}
*/

That code turns off the renderer component for each child object of the target, which you won't want to do if you'd like to see the animation!

Second, you'll want to stop the animation playback in the OnTrackingFound method. It should be as simple as this:

Animation myAnimation = (Animation) gameObject.GetComponentInChildren(typeof(Animation));
myAnimation.Stop();

- Kim

Re: OnTrackingLost() and OnTrackingFound()...

February 24, 2011 - 6:30am #17

Thank you very much Yes now im able to play animation onTrackingFound,now i wrote the same code inside onTrackinglost method and removed it from onTrackingFound even then it is playing animation ontracking found...my aim is to play some animation onTrackingLost or notify user with a message on the screen ..is there anyway??
This is the code i used
public void OnTrackingLost()
{

Renderer[] rendererComponents = GetComponentsInChildren();
foreach (Renderer component in rendererComponents) {
component.enabled = false;
}
MyJavaScript js=new MyJavaScript();

js.SayHello();

print("Trackable " + TrackableName + " lost");
}
This is the javascript i used
function Start ()
{
// Create the clip with the curve
var clip = new AnimationClip();
var curve = AnimationCurve.Linear(0, 0, 1,5);
clip.SetCurve("", Transform, "m_LocalPosition.x", curve);

// Add Event to animation clip
// This function can easily be called from an editor script
// to inject animation events into an animation clip
var event = new AnimationEvent();
event.time = 1;
event.functionName = "InstantiateParticle";
clip.AddEvent(event);

// Add clip to animation
animation.AddClip(clip, "test");
animation.wrapMode = WrapMode.PingPong;

// Add and play the clip
animation.Play("test");
}
@script RequireComponent(Animation)

Re: OnTrackingLost() and OnTrackingFound()...

February 23, 2011 - 6:23am #16

The problem is that your javascript is not attached to a GameObject with an Animation component. In fact, it isn't attached to any GameObject at all, because you are creating it using "new" instead of dragging it onto an object in your scene. You can do this, but then you'll have to pass in a valid GameObject with an Animation component, otherwise animation returns null.

function SayHello(myModel : GameObject) {
    print("1");
    var clip = new AnimationClip();
    print("2");
    var curve = AnimationCurve.EaseInOut(0, 0, 3, 10);
    print("3");
    clip.SetCurve("", Transform, "m_LocalPosition.x", curve);
    print("4");
    // Add Event to animation clip
    // This function can easily be called from an editor script
    // to inject animation events into an animation clip
    var event = new AnimationEvent();
    print("5");
    event.time = 1;
    print("6");
    //event.functionName = "InstantiateParticle";
    clip.AddEvent(event);
    print("7");
    // Add clip to animation	
    myModel.animation.AddClip(clip, "test");
    print("8");
    // Add and play the clip
    myModel.animation.Play("test");
    print("9");
}

Here's one way of finding an object with an Animation component that is attached to this target:

public void OnTrackingFound()
{
    ...
    
    Component myAnimation = gameObject.GetComponentInChildren(typeof(Animation));
    if (myAnimation != null) {
        MyJavaScript js=new MyJavaScript();
        js.SayHello(myAnimation.gameObject);
    }
}

- Kim

Re: OnTrackingLost() and OnTrackingFound()...

February 22, 2011 - 10:23pm #15

I tried it in the other way,i will share that with you that is also giving same null reference exception
This is the javascript in have put in the plugins
function SayHello() {
print("1");
var clip = new AnimationClip();
print("2");
var curve = AnimationCurve.EaseInOut(0, 0, 3, 10);
print("3");
clip.SetCurve("", Transform, "m_LocalPosition.x", curve);
print("4");
// Add Event to animation clip
// This function can easily be called from an editor script
// to inject animation events into an animation clip
var event = new AnimationEvent();
print("5");
event.time = 1;
print("6");
//event.functionName = "InstantiateParticle";
clip.AddEvent(event);
print("7");
// Add clip to animation
animation.AddClip(clip, "test");
print("8");
// Add and play the clip
animation.Play("test");
print("9");
}
I am calling the instance of javascript in the trackable found
MyJavaScript js=new MyJavaScript();
js.SayHello();
while debugging it printed upto 7
the later steps are giving null reference exception.

Re: OnTrackingLost() and OnTrackingFound()...

February 22, 2011 - 9:28pm #14

yaa i did everything,i have that animation in my model..its not working,if u have any such example with could you please share that with me

Re: OnTrackingLost() and OnTrackingFound()...

February 22, 2011 - 6:44am #13

Is myModel null? You have to drag an instance of your model from the Scene/Hierarchy onto the public field you exposed in the Inspector. Also make sure you actually have an animation named "CarAnim" in the model!

- Kim

Re: OnTrackingLost() and OnTrackingFound()...

February 22, 2011 - 3:59am #12

Hii ksiva,
No it is not working,I checked the ddms it is giving NULL REFERENCE EXCEPTION
at TrackableScript.onTrackingLost
at trackerScript.LateUpdate()
This is the code i have put inside ontrackingLost()
myModel.animation.Play("CarAnim");

Re: OnTrackingLost() and OnTrackingFound()...

February 21, 2011 - 6:46am #11

It seems like the simplest thing would be to create a public variable to hold your model:

public GameObject myModel;

Drag the instance of your model into this field in the Inspector.

Now, in the OnTrackingFound method you can just do something like this:

myModel.animation.Play();

And in OnTrackingLost you can do this:

myModel.animation.Stop();

Does that not work?

- Kim

Re: OnTrackingLost() and OnTrackingFound()...

February 20, 2011 - 10:56pm #10

Hii Ksiva
I tried the above javascript what you gave,it worked very fine.The problem now im facing is I have to play an animation,i.e the 3d object that i am showing on the screen when tracking is found should get animated when the tracking is lost.

Re: OnTrackingLost() and OnTrackingFound()...

January 11, 2011 - 4:55pm #9

Okay, I added your code to my javascript file. I'm still not seeing any errors, but I changed a couple of things to get this to work correctly at runtime. First, I dragged an instance of the MyJavascriptClass onto the ImageTarget object in the scene. Next I edited the TrackableScript class as follows:

private MyJavascriptClass js;

void Awake ()
{
    TrackerScript trackerScript = (TrackerScript) FindObjectOfType(typeof(TrackerScript));
    
    js = (MyJavascriptClass) this.GetComponent(typeof(MyJavascriptClass));
    
    trackerScript.RegisterTrackable(this);
    
    OnTrackingLost();
    
    if (this.transform != null)
        previousScale = new Vector2(this.transform.localScale.x, this.transform.localScale.z);
}

public void OnTrackingFound()
{
    Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();
    
    foreach (Renderer component in rendererComponents) {
        component.enabled = true;
    }
    
    js.ResumeGame();
    
    print("Trackable " + TrackableName + " found");
}

public void OnTrackingLost()
{
    Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();
    
    foreach (Renderer component in rendererComponents) {
        component.enabled = false;
    }
    
    js.PauseGame();
    
    print("Trackable " + TrackableName + " lost");
}

Give that a try. The MyJavascriptClass.js file is still in the Plugins folder.

- Kim

Re: OnTrackingLost() and OnTrackingFound()...

January 11, 2011 - 3:25pm #8
var MessageCenter : GameObject;

function Start(){
	MessageCenter = GameObject.FindWithTag ("MessageCenter");
}

function PauseGame() {
    Time.timeScale = 0;
    MessageCenter.gameObject.guiText.text = "Tracking Lost";
    print("hello");
}

function ResumeGame() {
    Time.timeScale = 1;
    MessageCenter.gameObject.guiText.text = "Tracking Found";
}

This is the contents of my MyJavascriptClass.js.

I'll revert back to your code and give it a look. I do seem to remember seeing your print come up. I may have goofed something up in the process of customizing your code. I'll let you know asap.

Thanks again,
CapnJ

Re: OnTrackingLost() and OnTrackingFound()...

January 11, 2011 - 9:29am #7

That's strange, this seems to work fine for me. Could you copy the contents of your MyJavascriptClass here, or send it to

? Perhaps there is something in the javascript that is causing an issue...

Have you tried just a bare bones javascript file with a simple print statement like I used above?

- Kim

Re: OnTrackingLost() and OnTrackingFound()...

January 10, 2011 - 5:42pm #6

Hmm, do i have this right then?

public void OnTrackingFound()
    {
    	MyJavascriptClass js = new MyJavascriptClass();
		js.ResumeGame();
    	
		Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();
		
		foreach (Renderer component in rendererComponents) {
			component.enabled = true;
		}
		
		print("Trackable " + TrackableName + " found");
	}
	
	public void OnTrackingLost()
    {
    	MyJavascriptClass js = new MyJavascriptClass();
		js.PauseGame();
    	
		Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();
		
		foreach (Renderer component in rendererComponents) {
			component.enabled = false;
		}
		
		print("Trackable " + TrackableName + " lost");
	}

Throwing an error somewhere. It gives me:

You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
UnityEngine.MonoBehaviour:.ctor()
MyJavascriptClass:.ctor() (at Assets/Plugins/Android/MyJavascriptClass.js:1)
TrackableScript:OnTrackingLost() (at Assets/Qualcomm Augmented Reality/Scripts/TrackableScript.cs:105)
TrackableScript:Awake() (at Assets/Qualcomm Augmented Reality/Scripts/TrackableScript.cs:77)

and..

NullReferenceException: Object reference not set to an instance of an object
MyJavascriptClass.PauseGame () (at Assets/Plugins/Android/MyJavascriptClass.js:5)
TrackableScript.OnTrackingLost () (at Assets/Qualcomm Augmented Reality/Scripts/TrackableScript.cs:106)
TrackableScript.Awake () (at Assets/Qualcomm Augmented Reality/Scripts/TrackableScript.cs:77)

o, and the .js file is in the plugins folder. (Tried it in the Plugins/Android folder as well, no dice)

Yar.:(

Re: OnTrackingLost() and OnTrackingFound()...

January 10, 2011 - 10:20am #5

AWESOME!!! I think this is exactly what i need. Thank you SOOOO much, would have never thought to need to throw it in the plugins folder.

I'll let you know how it goes!:)

Thanks again,
CapnJ

Re: OnTrackingLost() and OnTrackingFound()...

January 10, 2011 - 7:37am #4

http://unity3d.com/support/documentation/ScriptReference/index.Script%5Fcompilation%5F28Advanced29.html

http://www.unifycommunity.com/wiki/index.php?title=Head_First_into_Unity_with_JavaScript

After reading through those, I created a javascript file named MyJavascriptClass.js and placed it in the Plugins folder. I added a really simple function to this file:

function SayHello() {
    print("hello there!");
}

Then, in the TrackableScript.OnTrackingFound method I added the following:

MyJavascriptClass js = new MyJavascriptClass();
js.SayHello();

The important take away here is that your javascript files need to be compiled before the C# scripts. Placing them in the Plugins folder is one approach. Also, even if you do not explicitly create a class in your javascript file, a class is always created with the name of the file itself.

- Kim

Re: OnTrackingLost() and OnTrackingFound()...

January 9, 2011 - 2:06pm #3

Hello, Still having tons of trouble achieving this. Any examples on what to put in the TrackableScript.cs to reach out to an .js file for game pause functions? I'm assuming i'm not the first one to do this.

Thanks mates,
CapnJ

Re: OnTrackingLost() and OnTrackingFound()...

December 31, 2010 - 1:10pm #2

Those are callback functions, they get called when tracking is lost or found. You can add some code to the C# functions to call your own javascript functions elsewhere (assuming C# can make calls to javascript, I haven't tried this).

- Kim

Log in or register to post comments