How to set TrackableName for an ImageTarget at runtime in Vuforia SDK for Unity 3D? - unity3d

I am using Vuforia's SDK for Unity 3D platform. I am trying to set the TrackableName dynamically at runtime. I found a code
GameObject prefab = Instantiate(imageTarget) as GameObject;
ImageTargetBehaviour imgTargetBeh = prefab.GetComponent();
imgTargetBeh.DataSetPath = "QCAR/Test.xml";
imgTargetBeh.TrackableName = "Daddy";
imgTargetBeh.mInitializedInEditor = true;
myModel.transform.parent = prefab.transform;
The problem is I am getting error with DataSetPath, TrackableName, mInitializedInEditor.
The ImageTargetBehaviour class does not have above mentioned properties.
So how/where to set the properties?
Thanks,
Sris

I can't yet global comment, so I'll ask here for more info and update this when I have it.
The second line worries me, because you're not telling Unity what kind of component to get. Try GetComponent(typeof(ImageTargetBehaviour)) instead of the null parameters.
The problem is I am getting error with DataSetPath, TrackableName, mInitializedInEditor. The ImageTargetBehaviour class does not have above mentioned properties. So how/where to set the properties?
If the ImageTargetBehaviour class doesn't have these properties, then why are you trying to access them? Does the documentation instruct you to do so? If so, then something is wrong with your ImageTargetBehaviour.js script.

Related

How to disable the Tracked Pose Driver in a Unity VR Application?

I need to write a VR application that disables HMD positional tracking for a specific situation and then reenable it again.
In the UI it is a simple as ticking and unticking the TrackedPoseDriver shown in the picture below.
How can I do that via Scripting?
I assume I need to use the enabled property of a game object. But I don't know how to grab a hold of this GameObject (or Component).
EDIT: In case this was clear this is GameObject/Component associated to the main camera.
Ok. So I was able to do It.
What I did is I search MonoBehaviour Based Components on the camera and then cast it to a MonoBehaviour once I found the right one. And then used the enabled property. Like this:
Component[] components = Camera.main.GetComponents(typeof(MonoBehaviour));
bool found = false;
foreach(Component component in components) {
string name = component.ToString();
MonoBehaviour mb = (MonoBehaviour) component;
if (name.Contains(TRACK_POSE_DRIVER_NAME)){
mbTrackingPose = mb;
found = true;
break;
}
}
The unique string that the toString() method provided was somethign like (I don't have it on hand) "Main Camera (Unity.XR.TrackedPoseDriver)". So I just made sure that the string had the "TrackedPoseDriver" and stored that as a MonoBehaviour.
I hope this helps someone else.

Spatial Object Mesh Observer: Mesh Data

For an academy project I have to find parts of a object (location / angle relative to the object ). The object is marked with a QR-code.
Currently I'm stuck on the basics. I scanned a room and load this room with "Spatial Object Mesh Observer".
But this observer gives no relevant information:
The observer does not attempt to find 3D model LODs when sending the meshes to the application.
Someone a hint where I could start?
Scanned Room with a box ( object to find ):
var observer = CoreServices.GetSpatialAwarenessSystemDataProvider<IMixedRealitySpatialAwarenessMeshObserver>();
// Loop through all known Meshes
foreach (SpatialAwarenessMeshObject meshObject in observer.Meshes.Values)
{
Mesh mesh = meshObject.Filter.mesh;
var vertices = mesh.vertices;
// mesh.vertexCount -> 15978
// mesh.vertices -> empty
// mesh.triangles -> empty
// Do something with the Mesh object
}
Edit: 29.04.2021
Unity: Unity 2019.4.21f1
MRTK: 2.6.1
Everything seems to be loaded correctly and the triangles are recognized in an external tool.
However, this information is not available in Unity.
From our understanding, you can't obtain the vertices and triangles property of Spatial Mesh at the runtime. So, we try to make some modifications based on the SpatialAwarenessMeshDemo scene (Assets/MRTK/Examples/Demos/SpatialAwareness/Scenes) to reproduce this issue. We added the following code to the ToggleObservers() method and made it to be invoked when the sphere is clicked.
var observer = CoreServices.GetSpatialAwarenessSystemDataProvider<IMixedRealitySpatialAwarenessMeshObserver>();
foreach (SpatialAwarenessMeshObject meshObject in observer.Meshes.Values)
{
Mesh mesh = meshObject.Filter.mesh;
Debug.Log(mesh.vertexCount);
Debug.Log(mesh.vertices);
Debug.Log(mesh.triangles);
}
After our test, everything works well both in the HoloLens2 Device and Unity Holographic Remoting, it always outputs the expected value. So we recommend that you check for updates in Settings to see If there is a system update available for HoloLens 2. Then follow our steps to make a simple test to see if that works. This may help you locate the problem in your project.
I moved the code to "void OnBecameVisible()", now the information are available

what is the equivelant of get_global_pos from version 2.0 in 3.0?

I want to make a turret that is shooting lazers and i watched a tutorial on it. The person in the video used (scenename).position(get.node("Position2D").get_global_pos()) in order to spawn a projectile on the potition 2d. When i run this code, the game freezes and the debugger says "Invalid call. Nonexistant function 'get_global_pos' in base 2D". In the comments someone said that the tutorial is using 2.0 godot and that in 3.0 you dont use get_ and instead of pos you put potition, so i tried changing get_global_pos to global_position but it didnt work. i am very new to this and if you want any further information please let me know
As you already found out, global_position is the equivalent to get_global_pos.
The difference is, that global_position is no function but a member.
So instead of:
(scenename).position(get.node("Position2D").get_global_pos())
you have to write:
(scenename).position = get.node("Position2D").global_position

Unity is always throwing a Win32 Exception [duplicate]

so I'm new to Unity and I've been trying to test the scene with the script attatched to a character. However, it keeps saying "The associated script cannot be loaded. Please fix any compile errors and assign a valid script." It also says that the name of the file could be different from the name in the code but it isnt, and also it says that the code could be missing MonoBehaviour Scripts. It wont even allow me to attach the script to characters because it cant find the script class.
I've copied and downloaded character movement codes from the internet but they didnt work either. I've also tried deleting and re-making the CS files but that didnt work either. Even adding empty scripts to characters didnt work unless i do it from "Add component"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
SpriteRenderer sprite;
Rigidbody2D rigid;
// Start is called before the first frame update
void Start()
{
sprite = GetComponent<SpriteRenderer>();
rigid = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
if (Input.GetKey("d"))
rigid.velocity = new Vector2(2, 0);
else if (Input.GetKey("a"))
rigid.velocity = new Vector2(-2, 0);
}
}
There are also these errors in Unity if that helps
I think your class name is different from file name.
Unity apparently can't handle apostrophes (single-quote ') in the directory name of the editor. You need to get rid of the apostrophe in your directory name. Once you make that change, Unity should be able to build the scripts as intended.
Edit: This has been fixed in more recent versions - see https://issuetracker.unity3d.com/issues/scripts-do-not-get-compiled-if-the-unity-editor-path-contains-apostrophes for reference
First, it is recommended to use "Add component" to create a script, if you want to attach it to a GameObject, as it automatically imports necessary libraries. Implementing MonoBehaviour is necessary for adding a script to a GameObject.
Second, FixedUpdate() should not be set to private, it does not need an access modifier, just like Start(), see https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html.
Third, the errors in your first screenshot seem to imply that there is a problem with your Unity installation. Try reinstalling it and make sure that the Editor you install matches your operating system (64 or 32 bit?).
Fourth, the second screenshot is shown when you use any obsolete libraries or classes, which does not seem to be the case in the script you shared.
Hope that helps.
It's basically because you deleted some script or renamed it or degraded unity version. you might have to reassign the script at the required position/component.
Note: Make sure that class name is the same as the script name in unity.

Unity GetComponent(GUIText) bug?

I have an issue with the GetComponent(GUIText) the error i get is
There is no 'GUIText' attached to the "#######COUNTER(Clone)" game object, but a script is trying to access it.
Here is my code:
var UItecxt = GameObject.Find("#######COUNTER(Clone)");
var txtconvert = UItecxt.GetComponent(GUIText);
print(txtconvert);
txtconvert.text = counternumb.ToString();
I HAVE a GUIText on my clone! What is the issue? Thanks!
Your problem is that there is no GameObject named "#######COUNTER(Clone)" cloned in the scene. Run my code below and you will notice.
var UItecxt = GameObject.Find("#######COUNTER(Clone)");
var txtconvert : GUIText;
if(UItecxt != null)
txtconvert = UItecxt.GetComponent(GUIText);
else
Debug.Log("There was no GameObject with the name '#######COUNTER(Clone)' in the scene");
To fix it just make sure you do have a GameObject with that name.
Your question is not detail enough to determine the problem.
Still, i assumed the gameobject that you mentioned should be a prefab that you instantiated.
Where you put or attach the sciprt? Make sure it is attached to the prefab that you instantiated.
Also, you can try use direct reference the component instead of using gameobject.Find.
It is easier for you to drag and drop element at the inspector.