arfoundation-samples icon indicating copy to clipboard operation
arfoundation-samples copied to clipboard

How can I communicate with the ImageInfoPrefab from my scene?

Open SirLouLou opened this issue 6 years ago • 5 comments

Hi All,

I am a masterstudent Interaction Designer and working with Unity AR Foundations for my thesis. I am relatively new to Unity but used other games engines like Unreal Engine before. So sorry if this is a noob question, but haven't found a solution yet for the last week.

So I am experimenting in my thesis with Physical Computing & AR. Via the Uduino plugin I connected Unity with my Arduino through WIFI. The Uduino component and script have to be in the ImageTracking Scene. But I want stuff to happen in my ImageInfoPrefab based on the input I get from my Arduino. I tried everything but my scene-script and Prefabscript would not communicate with each other.

The problem that I found is that because the ImageInfoPrefab isn't in the scene at start (but is initiated trough the AR Tracked Image Manager Script) both scripts cannot communicate with each other. And I don't know the solution for that.

The code down below is a script that is in my scene. In this script all the data from my Arduino is read and pushed to Unity. In the following example a boolean goes to true if data = 0 and it works.

public Text textZone;
public  Boolean spelStart = false;
public static Boolean triggerBoolean = false;

// Start is called before the first frame update
void Start()

{
    UduinoManager.Instance.OnDataReceived += DataReceived;
    UduinoManager.Instance.alwaysRead = true; // This value should be On By Default
    UduinoManager.Instance.pinMode(1, PinMode.Input);

}
void DataReceived(string data, UduinoDevice board)
{
    Debug.Log(data);
    textZone.text = data;
    
    // So if the data from my Arduino says 0  the following boolean needs to be true
    if (data == "0")
    {
        Debug.Log("Spelinteractie 1 begint");
        spelStart = true;
        
    }

}

void Update() {
   
}

} `

The code below is from my ImageInfoPrefab. When the boolean = true something needs to happen. But not is happening even the debug.log isn't printing.

public MeshRenderer Vingerafdruk1;
public MeshRenderer Vingerafdruk2;


// Start is called before the first frame update
void Start()
{

    
    //Vingerafdruk
    
    Vingerafdruk1.gameObject.SetActive(false);
    Vingerafdruk1.enabled = false;

    Vingerafdruk2.gameObject.SetActive(false);
    Vingerafdruk2.enabled = false;

}

// Update is called once per frame
void Update()
{
    
    if (spelStart == true) {
        Debug.Log("Spelstart is nu op true");
        Vingerafdruk1.gameObject.SetActive(true);
        Vingerafdruk1.enabled = true;

    }
    
   
    
}

}

I dropped some screenshots down below.

Thx a lot in advance.

image

image

SirLouLou avatar Apr 09 '20 12:04 SirLouLou

Hi,

Could someone help me please. Am still stuck :/

How can I efficiently let the ImageTracking Scene communicate with the ImageInfoPrefab.

Tyvm.

SirLouLou avatar Apr 13 '20 14:04 SirLouLou

So basically to send information in to the prefab you need to use GameObject.GetComponent().MethodInPrefabScript(). Basically write a method that accepts a parameter and performs some action in the prefab and include in a monobehavior attached to your base component of your prefab. If you search "Unity GetComponent" there are several tutorials on this.

So assuming the script in your prefab is called "PrefabScript" then you would use in the scene code `private GameObject ImageInfoPrefab bool spellStart = true;

ImageInfoPrefab = _trackImageManager.trackedImagePrefab; ImageInfoPrefab.GetComponent().SetSpellStart(spellStart);`

and in the Prefab script have a method

`private bool SpellStart;

public void SetSpellStart(bool IsStarting) { SpellStart = IsStarting; }`

There may be a way to set the variable more directly - I am still somewhat new to Unity as well as well but this worked for me.

jiungerich avatar May 25 '20 21:05 jiungerich

Tyvm @jiungerich for your help. Appreciate it very much.

But I get some errors if I do what you told me to do. I'm still very new in Unity, so I hope you bare with me and would check the code / errors below if you see something that I did wrong.

Tyvm

ERRORS

Severity Code Description Project File Line Suppression State Error CS0103 The name '_ARtrackImageManager' does not exist in the current context Severity Code Description Project File Line Suppression State Error CS0411 The type arguments for method 'GameObject.GetComponent()' cannot be inferred from the usage. Try specifying the type arguments explicitly. Severity Code Description Project File Line Suppression State Error CS0120 An object reference is required for the non-static field, method, or property 'ARTrackedImageManager.trackedImagePrefab'

SCENE SCRIPT

public class ReadKeypad : MonoBehaviour 
{

public Text textZone;

private GameObject ImageInfoPrefab;
bool spellStart = true;




// Start is called before the first frame update
void Start()
{

    UduinoManager.Instance.OnDataReceived += DataReceived;
    UduinoManager.Instance.alwaysRead = true; // This value should be On By Default
    UduinoManager.Instance.pinMode(1, PinMode.Input);

    ImageInfoPrefab = _ARtrackImageManager.trackedImagePrefab;
    ImageInfoPrefab.GetComponent().SetSpellStart(spellStart);

    //should it be this?

    ImageInfoPrefab = ARTrackedImageManager.trackedImagePrefab;

  

}
void DataReceived(string data, UduinoDevice board)
{
    UnityEngine.Debug.Log(data);
    textZone.text = data;
    


    if (data == "2")
    {
        UnityEngine.Debug.Log("Spelinteractie 1 begint");
 


    }

 
}

void Update() {
   
}

} ` PREFAB SCRIPT

public class kepadTriggers : MonoBehaviour
{

private bool SpellStart;



// Start is called before the first frame update
void Start()
{




}

void Update()
{
   
}

public void SetSpellStart(bool isStarting)
{
    SpellStart = isStarting;
}
}

SirLouLou avatar Jun 05 '20 18:06 SirLouLou

Having an issue actually posting full code because github edits out necessary carets needed in the code. S You need to pull the ARtrackedImageManager into your script - that is why you are having the error.
The debug log is telling you the AR tracked image manager is missing.

The monobehaivor code that you are writing should be attached to AR session origin with the ArTrackedImageManager as a component. Then include:

Capture

It is also possible to instantiate AR tracked image manager in script but I have had issues in doing so on android devices.

*Editted because code was not showing properly

jiungerich avatar Jun 07 '20 18:06 jiungerich

@SirLouLou
Just wondering if jiungerich's solution has solved the issue? If not, let me know if you are still getting the errors mentioned your post above.

tropicdragon avatar Jul 08 '20 18:07 tropicdragon