Log in or register to post comments

How do i change a object that has been loaded on a target?

February 27, 2017 - 11:35am #1

Hello,

I'm loading a prefab called myModelPrefab onto a target during runtime using the script below. How would i now change between different prefabs that are located in my Resources file when i press a button? 

 

using UnityEngine;

using System.Collections;

using Vuforia;

public class MyModelInstantiator : MonoBehaviour, ITrackableEventHandler {

  private TrackableBehaviour mTrackableBehaviour;

  public Transform myModelPrefab;

  // 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.DETECTED ||

        newStatus == TrackableBehaviour.Status.TRACKED ||

        newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)

    {

      OnTrackingFound();

    }

  }

  public void ButtonPressed(){

  }

  private void OnTrackingFound()

  {

    if (myModelPrefab != null)

    {

      Transform myModelTrf = GameObject.Instantiate(myModelPrefab) as Transform;

      myModelTrf.parent = mTrackableBehaviour.transform;

      myModelTrf.localPosition = new Vector3(0f, 0f, 0f);

      myModelTrf.localRotation = Quaternion.identity;

      myModelTrf.localScale = new Vector3(0.0005f, 0.0005f, 0.0005f);

      myModelTrf.gameObject.SetActive(true);

    }

  }

}

Log in or register to post comments