x


Dynamically instantiated game objects not showing in main camera (with background camera)

Hi all,

I have followed the instructions here to create a background image in a second camera. The main camera is set to ignore the background layer (culling mask has everything except background), and the background camera is set to only see the background image (only background is set in culling mask).

My code instantiates many game objects dynamically. These are not visible in my scene - it looks like they are behind my GUITexture because when I click run in the editor I can see them in the scene window, just not in the game window.

Any ideas?

more ▼

asked Aug 06 '10 at 05:12 AM

pixelthis gravatar image

pixelthis
1 1 1 3

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

I have done some more testing, and this seems to be an editor quirk/bug.

For example, quitting and restarting Unity resolves this issue - until I change the scene view (e.g. zoom in/out), after which it stops working again. Making an unrelated code change in a script then fixed it again. And so on...

I am using Unity Free (2.6) on Windows Vista.

more ▼

answered Aug 08 '10 at 10:43 PM

pixelthis gravatar image

pixelthis
1 1 1 3

Sounds like maybe you haven't set the camera depths properly. Having two cameras with the exact same depth results in random behavior like that, since there's no deterministic way to tell what camera will render on top.

Aug 08 '10 at 11:01 PM Eric5h5

The cameras have different depths and are in different locations. Should I set the background camera to have the shortest depth?

Aug 09 '10 at 07:53 AM pixelthis

The background camera should have the lowest depth, so set it at depth of -500 just to be sure you're not getting confused (yes, that's a minus sign).

Aug 09 '10 at 02:11 PM Cyb3rManiak
(comments are locked)
10|3000 characters needed characters left

In the mean while, if you want to try another way while you're designing the game - try creating this C# script (CreateBGPlaneOnCamera.cs) and attaching this to your BG camera. Set nBackgroundLayerID to the BG layer number, and assign a texture to txBackground. It should get ya there quick and dirty.

When you're ready to stop playing around, just return to using the GUILayer approach, since I don't think that quirk/bug will happen in a built player.

using UnityEngine;
using System.Collections;

public class CreateBGPlaneOnCamera : MonoBehaviour 
{
    public Texture2D    txBackground;
    public int          nBackgroundLayerID;

    void Start () 
    {
        GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
        plane.renderer.material.shader = Shader.Find("Transparent/VertexLit");
        plane.renderer.material.color = new Color(0.5f, 0.5f, 0.5f, 1f);
        plane.renderer.material.SetColor("_Emission", new Color(0.5f, 0.5f, 0.5f, 1f));
        plane.renderer.material.mainTexture = txBackground;
        plane.gameObject.layer = nBackgroundLayerID;
        plane.transform.parent = transform;
        plane.transform.localScale = new Vector3(Screen.width * 0.1f, 1f, Screen.height * 0.1f);
        plane.transform.localRotation = Quaternion.Euler(-90f, 0f, 0f);
        plane.transform.localPosition = new Vector3(0f, 0f, Screen.height * 0.5f / Mathf.Tan(camera.fov * 0.5f * Mathf.Deg2Rad));
        camera.farClipPlane = plane.transform.localPosition.z;
    }
}
more ▼

answered Aug 09 '10 at 02:55 PM

Cyb3rManiak gravatar image

Cyb3rManiak
1.6k 1 4 17

(comments are locked)
10|3000 characters needed characters left

you should put your new objects into its layer. like this:

gameObject.layer = 5; // 5 is the number of its layer
more ▼

answered Aug 06 '10 at 06:38 AM

AliAzin gravatar image

AliAzin
2.5k 42 56 79

I don't think that is the problem. Does Instantiating a game object place it on my user-defined background layer by default?

Aug 08 '10 at 08:32 PM pixelthis

Thanks for your help AliAzin - in the end it turns out it is probably editor related.

Aug 08 '10 at 10:44 PM pixelthis
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3015
x1683
x478
x167
x147

asked: Aug 06 '10 at 05:12 AM

Seen: 2126 times

Last Updated: Aug 06 '10 at 05:12 AM