0
votes

I am trying make my prefabs appear at runtime on my imagetarget by following this link vuforia instantiate prefab on imagetarget dynamically

the only difference is that I want to be able to drag some prefabs that I created instead of only one prefab shown in the tutorial.

public GameObject[] prefabModels;

my problem is even after my imagetarget is detected all my 3d prefabs didn't show up.

this code is attached to my imagetarget object

using System;
using UnityEngine;
using System.Collections;
using Vuforia;

public class ImageTargetMgr : MonoBehaviour, ITrackableEventHandler {

private TrackableBehaviour mTrackableBehaviour;

public GameObject[] prefabModels;

// Use this for initialization
void Start () {                
    mTrackableBehaviour = GetComponent<TrackableBehaviour>();

    if (mTrackableBehaviour)
    {
        mTrackableBehaviour.RegisterTrackableEventHandler(this);
    }        
}

// Update is called once per frame
void Update () {

}

public void OnTrackableStateChanged(TrackableBehaviour.Status previousStatus, TrackableBehaviour.Status newStatus)
{
    if (newStatus == TrackableBehaviour.Status.TRACKED || newStatus == TrackableBehaviour.Status.DETECTED || newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
    {
        OnTrackingFound();
    }
    else
    {
        OnTrackingLost();
    }
}

private void OnTrackingLost()
{

}

private void OnTrackingFound()
{
    Debug.Log("Image Target Found!");
    foreach (GameObject o in prefabModels)
    {
        if (o != null)
        {
            Debug.Log("Currently instantiated models is " + o.name);

            GameObject myPrefab = Instantiate(o, Vector3.zero, Quaternion.identity) as GameObject;

            myPrefab.transform.parent = mTrackableBehaviour.transform;
            myPrefab.transform.localPosition = new Vector3(0.0f, 10.0f, 0.0f);
            myPrefab.transform.localRotation = Quaternion.identity;
            //o.transform.localScale = new Vector3(1000.0f, 1000.0f, 1000.0f);

            myPrefab.gameObject.SetActive(true);

        }

    }

}

here are the log and what shown on hierarchy when running the scene

hope I will get some helps here..cheers

enter image description here

enter image description here

1
Have you assigned any prefabs to prefabModels array?Rob
Yes...i hv assigned some prefabsMuadzir Aziz
Does it ever call OnTrackingFound ? and log out "Image Target Found!" and "Currently instantiated models is"?Rob
It did. OnTackingFound fired but somehow those prefabs didn't appear. Checked the log and printed out both test strings.Muadzir Aziz
I noticed there is an instance of Glowtab(Clone) as seen on the second pic. However the object didn't seem to be rendered on the image target.Muadzir Aziz

1 Answers

0
votes

The problem finally solved after I added Box Collider component to the prefab object. I have to tweak also some of the transform settings.

Below are the screen captures of the prefab's inspector panel at runtime enter image description here

enter image description here