unity-game-engineunity-components

GameObject (Prefab object ) created by script does not appear in Game View but appears in Scene View in Unity3d


I created a pin object by script attached it to a sphere object .

using UnityEngine;

public class InstantiateMarkerPin : MonoBehaviour
{
    public float Xpos;
    public float Ypos;
    public float Zpos;
    public GameObject gameObjectPinInstantiate;

    // Start is called before the first frame update
    private void Start()
    {
        Xpos = 0.09f;
        Ypos = 0.50f;
        Zpos = 1.1f;
        //The original object, where to instantiate, and the orientation of the new object
        GameObject marker = (GameObject)Resources.Load("gameObjectPin");
        Vector3 location = new Vector3(Xpos, Ypos, Zpos);
        Quaternion rotation = Quaternion.Euler(0, 0, 0);
        //The object the script is attached to
        GameObject world = this.gameObject;
        //Instantiate the prefab
        gameObjectPinInstantiate = Instantiate(marker, location, rotation, world.transform);

        Debug.Log("InstantiateMarkerPin class : Marker  Location 2 :X, Y, Z : " + gameObjectPinInstantiate.transform.position);
    }

    // Update is called once per frame
    private void Update()
    {

    }
}

This script is attached to the sphere Object .My sphere Object have shader material of earth image (globe). This Instantiated Prefabs (gameObjectPin) on sphere surface appears on scene but not on game screen ,When I select the camera object in the camera preview also this object does not appear .

Scene view

Scene view

Scene View when camera is selected

Scene View when camera is selected

I am new to Unity what should I check or correct to appear my created object on the sphere basically I am trying to add pins to corresponding country and label it .Similar to the globe on this http://kitsdmcc.com/news

Gameobject is created when Play is clicked on the sphere object

Gameobject is created when Play is clicked on the sphere object

When the Pin Object is selected on play mode

When the Pin Object is selected on play mode


Solution

  • Oh now I see it! What you did was only setting its GIZMO via this menu

    enter image description here

    which is only displayed in the SceneView.

    This has nothing to do with the rendering in the GameView but is just a way for easier seeing and finding certain types of objects in the SceneView since usually they would be invisible if not selected.

    From the Glossary:

    A graphic overlay associated with a GameObject in a Scene , and displayed in the Scene View . Built-in scene tools such as the move tool are Gizmos , and you can create custom Gizmos using textures or scripting. Some Gizmos are only drawn when the GameObject is selected, while other Gizmos are drawn by the Editor regardless of which GameObjects are selected.


    As noted in the comments there is no Component at all on your GameObject so nothing is rendered in the Gameview.

    Of course now you could enable Gizmos also for the GameView via the Gizmos toggle

    enter image description here

    but I guess what you rather are trying to achieve is rather rendering that icon in the final App.


    You probably would like to use e.g. the SpriteRenderer component here. And simply drag in your Icon to the Sprite property.

    You might have to change the Pin Texture's TextureType to Sprite (2D and UI).

    enter image description here


    In general I would also recommend to Create a Prefab instead of using the Resources folder here.


    There are also some changes I would do to your code in general:

    public class InstantiateMarkerPin : MonoBehaviour
    {
        [Header("Settings")]
    
        // directly use a Vector3 for setting the values
        //                              | default value for this field 
        //                              | (only valid until changed via Inspector!)
        //                              v
        public Vector3 TargetPosition = new Vector3(0.09f, 0.5f, 1.1f);
        // Instead of using Resources simply reference the 
        // created Prefab here
        public GameObject gameObjectPinPrefab;
    
        [Header("Outputs")]
        public GameObject gameObjectPinInstantiate;
    
        private void Start()
        {
            // Careful this currently ovewrites any value set via the
            // Inspector. Later you will probably want to remove this.
            TargetPosition = new Vector3(0.09f, 0.5f, 1.1f);
    
            //As said I would rather use a prefab here and simply reference it above in the field
            //gameObjectPinPrefab = (GameObject)Resources.Load("gameObjectPin");
    
            //Instantiate the prefab
            gameObjectPinInstantiate = Instantiate(gameObjectPinPrefab, TargetPosition, Quaternion.identity, transform);
    
            Debug.Log("InstantiateMarkerPin class : Marker  Location 2 :X, Y, Z : " + gameObjectPinInstantiate.transform.position);
        }
    
        // Always remove empty Unity methods
        // They are called as messages if they exist 
        // and only cause unnecessary overhead
    }