Log in or register to post comments

Creating a child object of Image targets at runtime

April 7, 2012 - 10:40am #1

How should i create a child object of Image targets at runtime?

Creating a child object of Image targets at runtime

March 1, 2013 - 12:55am #12

Hi, you may want to hide (make invisible) your 3D model at start; for instance, you could add some code in the Start() method of your script like:

void Start() {

  hideMyModel();

}

To hide or show a 3D model you can use some code like the one below:

private void showObject(GameObject go)
    {
        Renderer[] rendererComponents = go.GetComponentsInChildren<Renderer>();
        Collider[] colliderComponents = go.GetComponentsInChildren<Collider>();

        // Enable rendering:
        foreach (Renderer component in rendererComponents)
        {
            component.enabled = true;
        }

        // Enable colliders:
        foreach (Collider component in colliderComponents)
        {
            component.enabled = true;
        }
    }


    private void hideObject(GameObject go)
    {
        Renderer[] rendererComponents = go.GetComponentsInChildren<Renderer>();
        Collider[] colliderComponents = go.GetComponentsInChildren<Collider>();

        // Disable rendering:
        foreach (Renderer component in rendererComponents)
        {
            component.enabled = false;
        }

        // Disable colliders:
        foreach (Collider component in colliderComponents)
        {
            component.enabled = false;
        }
    }

HTH.

 

Creating a child object of Image targets at runtime

March 1, 2013 - 12:36am #11

 

Thank you :)

I got my model under my image target. But my model displays on the scene without image target at start. After i have used imagetarget once it is normal.

What can i do so the model does not display before the imagetarget is used ?

Thanks again

Creating a child object of Image targets at runtime

February 28, 2013 - 5:54am #10

Hi, you are using the PrimitiveType.Box; this does not exist in Unity, you should use PrimitiveType.Cube (even if your variable is called Box).

Also, you are defining a public GameObject Box, but you are creating a local object called Box as well in this line:

GameObject Box = GameObject.CreatePrimitive(PrimitiveType.Cube);

you should replace that line with this:

Box = GameObject.CreatePrimitive(PrimitiveType.Cube);

In shoirt, this is the working script; however, these are very basic Unity programming aspects (not about Vuforia); you should probably post these type of questions in the Unity3D Forum.... (for next time..)

public class ModelPopup : MonoBehaviour {

    private bool mAddModel = false;

public GameObject Box;

    void OnGUI() {

        if (GUI.Button(new Rect(50,50,200,100), "Add Cube")) {

            mAddModel = true;
        }
    }

    void Update () {
        if (mAddModel) {
            Debug.Log ("Adding model...");

            Box = GameObject.CreatePrimitive(PrimitiveType.Cube);

            Box.transform.parent = this.gameObject.transform;

            // Adjust scale and position 
            // (use localScale and localPosition to make it relative to the parent)
            Box.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
            Box.transform.localPosition = new Vector3(0, 0.3f, 0);
            Box.transform.localRotation = Quaternion.identity;

            Box.gameObject.SetActive(true);

            mAddModel = false;
        }
    }
}
 
 

Creating a child object of Image targets at runtime

February 28, 2013 - 5:37am #9

 

using UnityEngine;

 

public class ModelPopup : MonoBehaviour {

     

    private bool mAddModel = false;

public GameObject Box;

     

    void OnGUI() {

        if (GUI.Button(new Rect(50,50,200,100), "Add Cube")) {

            mAddModel = true;

        }

    }

     

    // Update is called once per frame

    void Update () {

        if (mAddModel) {

            Debug.Log ("Adding model...");

             

            GameObject Box = GameObject.CreatePrimitive(PrimitiveType.Box);

             

            Box.transform.parent = this.gameObject.transform;

             

            // Adjust scale and position 

            // (use localScale and localPosition to make it relative to the parent)

            Box.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);

            Box.transform.localPosition = new Vector3(0, 0.3f, 0);

            Box.transform.localRotation = Quaternion.identity;

             

            Box.gameObject.SetActive(true);

             

            mAddModel = false;

        }

    }

}

Creating a child object of Image targets at runtime

February 28, 2013 - 5:30am #8

Hi, can you be more specific ? is the Box class something you created ? any code to share ?

Creating a child object of Image targets at runtime

February 28, 2013 - 5:25am #7

Get this error:

cannot convert 'object' expression to type 'UnityEngine.PrimitiveType'

I created a public GameObject Box; And used this Box insted of cube.

 

Creating a child object of Image targets at runtime

February 28, 2013 - 5:16am #6

Ok, let us know how it goes. By experience I know that sometime the issue might be just about setting an appropriate localScale in the object transform (like I do in my sample code).

 

Creating a child object of Image targets at runtime

February 28, 2013 - 5:07am #5

Ok,

Thank you AlessandroB!

Will see what i can do with this script! Right now i have a script that retrives a 3d-model from my ftp server and displays it in my scene.

My problem is that it does not display the object inside the imagetarget. So hopefully this will solve my problem :)

Best Regards

Creating a child object of Image targets at runtime

February 28, 2013 - 4:59am #4

Hi neliuz,

if you start from the ImageTargets sample,

create a script like the one here below (for instance call it ModelPopup.cs), and attach it to the ImageTarget (note: in this example, you have a button to trigger the action to attach a simple cube):

public class ModelPopup : MonoBehaviour {
	
	private bool mAddModel = false;
	
	void OnGUI() {
		if (GUI.Button(new Rect(50,50,200,100), "Add Cube")) {
			mAddModel = true;
		}
	}
	
	// Update is called once per frame
	void Update () {
		if (mAddModel) {
			Debug.Log ("Adding model...");
			
			GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
			
			cube.transform.parent = this.gameObject.transform;
			
			// Adjust scale and position 
			// (use localScale and localPosition to make it relative to the parent)
			cube.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
			cube.transform.localPosition = new Vector3(0, 0.3f, 0);
			cube.transform.localRotation = Quaternion.identity;
			
			cube.gameObject.SetActive(true);
			
			mAddModel = false;
		}
	}
}

HTH.

Creating a child object of Image targets at runtime

February 28, 2013 - 4:29am #3

Hi, Thanks for the usefull info.

But one question :) 

Where do i define this line of code? In witch script file?

Thanks

Re: Creating a child object of Image targets at runtime

April 7, 2012 - 1:28pm #2

Simply define the ImageTarget as the transform.parent of the child object..

e.g.
targetObject.transform.parent = imageTarget.transform;

now targetObject is the child of imageTarget.

Log in or register to post comments