Log in or register to post comments

Dynamically assigning game objects as children of VuMark at runtime in Unity

March 15, 2018 - 4:13am #1

I am working on an AR application for Android in Unity and as I will only know what objects I want to be tracked and displayed at runtime, I am currently trying to assign these objects (which are created dynamically) as the child of VuMark at runtime accordingly.

However, nothing shows up when I test this in the Unity editor by showing the VuMark marker I have in front of my webcam! Strangely enough, if I manually instantiate an object (like a cube) at the hierarchy view as the child of VuMark, the cube would show up! I am really at my wits' end as to what I can do to make my dynamically created objects show up, since what my code is doing is essentially the same as what I did manually. Here's the code I used to add the dynamically created object as child of VuMark:

void Start()
{
  GameObject vuMark = GameObject.Find("VuMark");

  GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
  
  // "material" is a public instance variable I have outside of this snippet
  cube.GetComponentInChildren<MeshRenderer>().material = material;
  cube.transform.parent = vuMark.transform;
}

Any suggestions on what I should try next would be much appreciated! Thanks!

Dynamically assigning game objects as children of VuMark at runtime in Unity

March 21, 2018 - 3:46am #3

I've just tried using prefab, and soon realised that the problem was the primitive created in code was too big/not a unit cube - it was instead 10x the unit cube - so the camera was inside the cube and as a result the object was not visible... silly me. Hopefully this might help someone out in future to read this as a gotcha if they are just starting out with Vuforia!

Dynamically assigning game objects as children of VuMark at runtime in Unity

March 19, 2018 - 8:40am #2

Have u tried using Instanciate() for instanciating a Prefab instead of creating a primitive?

 

https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

Log in or register to post comments