0

I have two script simplecloudhander.cs cloudtarget.cs

simplecloudhander.cs

public string mTargetMetadata = "";

public void OnNewSearchResult(TargetFinder.TargetSearchResult targetSearchResult)
    {

        GameObject newImageTarget = Instantiate(ImageTargetTemplate.gameObject) as GameObject;

        GameObject augmentation = null;

        string model_name = targetSearchResult.MetaData;


        if( augmentation != null )
            augmentation.transform.parent = newImageTarget.transform;


        ImageTargetAbstractBehaviour imageTargetBehaviour = mImageTracker.TargetFinder.EnableTracking(targetSearchResult, newImageTarget);

        Debug.Log("Metadata value is " + model_name );
        mTargetMetadata = model_name;
}

i want to access mTargetMetadata value in another cloudtarget.cs script

here cloudtarget.cs script

void OnGUI() {
        SimpleCloudHandler sc = new SimpleCloudHandler ();

        GUI.Label (new Rect(100,300,300,50), "Metadata: " + sc.mTargetMetadata);

}

but i can't get mTargetMetadata value in another script

Vijay
  • 663
  • 4
  • 15
JAY PATEL
  • 19
  • 2
  • 4

2 Answers2

2

You have to add a reference to that script in the cloudtarget script. Just add a public variable of type SimpleCloudHandler to the class cloudtarget, not on the OnGUI method, later drag and drop the GameObject with the SimpleCloudHandler atached to the script cloudtarget in the inspector.

Example:

enter image description here enter image description here

Drag and drop the MainCamera with the SimpleCloudHandler script attached => to the public SimpleCloudHandler variable of the cloudtarget script through the inspector.

There are multiple ways to make a reference in Unity, I recommend you to look to the documentation that Unity offer

Sergio Ormeño
  • 389
  • 1
  • 8
0

if your scripts are attached to the same object then you can use GetComponentlike this,

gameObject.GetComponent<simplecloudhander>().mTargetMetadata

if they are not attached to the same gameObjectthe you can use GameObject.Find first to find the gameObject that the script is attached to, you should use the name of the gameObject to find it, then use the GetComponent to get the componentthat you want

GameObject.Find("nameOftheGameObject").GetComponent<simplecloudhander>().mTargetMetadata

also there is no need for new keyword, if you just want to access it,

 simplecloudhander sch;

    void Start()
    {
     sch = GameObject.Find("nameOftheGameObject").GetComponent<simplecloudhander>();
    }
    void OnGUI() {
            GUI.Label (new Rect(100,300,300,50), "Metadata: " + sch .mTargetMetadata);

    }
Milad Qasemi
  • 3,011
  • 3
  • 13
  • 17
  • thanxx sir i try it but its don't retrive it – JAY PATEL Jan 10 '17 at 15:11
  • thanxx sir i try it but its don't retrive it void OnGUI() { SimpleCloudHandler sc = new SimpleCloudHandler (); a= GameObject.Find ("mTargetMetadata").GetComponents().ToString(); GUI.Label (new Rect(100,300,300,50), "Metadata: " + a); } – JAY PATEL Jan 10 '17 at 15:24
  • you shouldnt call it in `OnGUI` , I edited to show you where it should be called, also is `mTargetMetadata` also the name of your gameobject? – Milad Qasemi Jan 10 '17 at 15:41
  • https://www.datafilehost.com/d/c1f60b49 https://www.datafilehost.com/d/3386727d i already here two file 1.SimpleCloudHandler.cs 2.VideoPlaybackBehaviour.cs in 1st file simplecloudhandler.cs connect with vuforia cloud database and when imagetarget scan and it show 3D object also show meta data value in GUI LABEL it's work bt i want to first scirpt in metadata value (mTargetMetadata) access in second script and display using GUIlable – JAY PATEL Jan 11 '17 at 09:34