- Home /
ARCore augmented image - The Object you want to instantiate is null
I want to create a app that will recognise images and add different objects to them. I'm working off the Google augmentedimage example.
I have two objects sphere and a cube. Both are prefabs and both have AugmentedImageVisualizer assigned to them.
AugmentedImageExampleController separates images as it should but when AugmentedImageVisualizer wants to enable visibility for cube or sphere it fails with error The object you want to instantiate is null.
Here are scripts
public class AugmentedImageExampleController : MonoBehaviour
{
/// <summary>
/// A prefab for visualizing an AugmentedImage.
/// </summary>
public AugmentedImageVisualizer[] AugmentedImageVisualizerPrefab;
/// <summary>
/// The overlay containing the fit to scan user guide.
/// </summary>
public GameObject FitToScanOverlay;
private Dictionary<int, AugmentedImageVisualizer> m_Visualizers
= new Dictionary<int, AugmentedImageVisualizer>();
private List<AugmentedImage> m_TempAugmentedImages = new List<AugmentedImage>();
/// <summary>
/// The Unity Update method.
/// </summary>
public void Update()
{
// Exit the app when the 'back' button is pressed.
if (Input.GetKey(KeyCode.Escape))
{
Application.Quit();
}
// Get updated augmented images for this frame.
Session.GetTrackables<AugmentedImage>(
m_TempAugmentedImages, TrackableQueryFilter.Updated);
// Create visualizers and anchors for updated augmented images that are tracking and do
// not previously have a visualizer. Remove visualizers for stopped images.
foreach (var image in m_TempAugmentedImages)
{
int i = image.DatabaseIndex;
AugmentedImageVisualizer visualizer = null;
m_Visualizers.TryGetValue(image.DatabaseIndex, out visualizer);
Debug.Log(image.DatabaseIndex);
if (image.TrackingMethod == AugmentedImageTrackingMethod.FullTracking && visualizer == null)
{
// Create an anchor to ensure that ARCore keeps tracking this augmented image.
Anchor anchor = image.CreateAnchor(image.CenterPose);
Debug.Log("Anchor created");
visualizer = (AugmentedImageVisualizer)Instantiate(
AugmentedImageVisualizerPrefab[i], anchor.transform);
visualizer.Image = image;
m_Visualizers.Add(image.DatabaseIndex, visualizer);
}
else if (image.TrackingMethod == AugmentedImageTrackingMethod.NotTracking && visualizer != null)
{
m_Visualizers.Remove(image.DatabaseIndex);
GameObject.Destroy(visualizer.gameObject);
}
}
// Show the fit-to-scan overlay if there are no images that are Tracking.
foreach (var visualizer in m_Visualizers.Values)
{
if (visualizer.Image.TrackingMethod == AugmentedImageTrackingMethod.FullTracking)
{
FitToScanOverlay.SetActive(false);
return;
}
}
FitToScanOverlay.SetActive(true);
}
}
and Visualiser script
public class AugmentedImageVisualizer : MonoBehaviour
{
/// <summary>
/// The AugmentedImage to visualize.
/// </summary>
public AugmentedImage Image;
public GameObject prefabToInstantiate;
/// <summary>
/// The Unity Update method.
/// </summary>
public void Update()
{
if (Image == null || Image.TrackingMethod != AugmentedImageTrackingMethod.FullTracking)
{
prefabToInstantiate.SetActive(false);
return;
}
float halfWidth = Image.ExtentX / 2;
float halfHeight = Image.ExtentZ / 2;
prefabToInstantiate.transform.localPosition =
(halfWidth * Vector3.left) + (halfHeight * Vector3.back);
prefabToInstantiate.SetActive(true);
}
}
I tried to solve the problem with instantiating it at start with Instantiate()
In visualizer but it still failed. Original example doesn't use it it just enables gameobject's visibility.
Screenshots: Sphere and cube are the same:
What is this for a ToolKit you are using? I would try to use ARFoundation. This is the new Unity standard.
Answer by KirillKuzyk · Jul 14, 2020 at 05:36 AM
You can't Instantiate MonoBehavior separately from GameObject. Please Instantiate the whole GameObject and then call instantiatedGameObject.GetComponent();