- Home /
Question by
FelixCova · Feb 11 at 03:48 PM ·
scripting problemgameobjectsaugmented realitycomponents
Attach the ObjectManipulator MRTK component in runtime to instantiated GameObject?
I am building an XR experience for Mobile platforms. For this, I import a glTF model to my scene through a GameObject when it loads and instantiate it in the scene. This part is ok.
What I want to achieve is that after the GameObject containing the model is instantiated, I want to attach an MRTK script to it, but when I try to use the GameObject.AddComponent<> method I can't see the name of the script.
Is there any specific way to reference the script if it is within my Assets?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Microsoft.MixedReality.Toolkit.XRSDK.Input;
public class ObjectManager : MonoBehaviour
{
private GameObject placedModel;
//This runs first
void OnEnable()
{
Debug.Log("OnEnable called");
SceneManager.sceneLoaded += OnSceneLoaded;
}
//this runs second
async void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
Debug.Log("OnSceneLoaded: " + scene.name);
var gltf = new GLTFast.GltfImport();
var success = await gltf.Load("https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/Duck/glTF/Duck.gltf");
if (success)
{
// Instantiate the glTF's main scene
gltf.InstantiateMainScene(new GameObject("PlacedModel").transform);
}
else
{
Debug.LogError("Loading glTF failed!");
}
}
//this runs third
private void Start()
{
placedModel = GameObject.Find("PlaceModel");
placedModel.AddComponent<ScriptName>;
}
}
Comment