0

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

howcheng
  • 2,211
  • 2
  • 17
  • 24
Jerry Abraham
  • 103
  • 12
  • Can you add a screenshot with the pin selected? – Camile Nov 13 '19 at 09:15
  • @Camile I added , can I get any idea why it does not appearing in game screen ?I am sitting for this for two days .Please help Thanks in advance – Jerry Abraham Nov 13 '19 at 09:21
  • 2
    It looks like your object is just an empty transform? The graphical image of a pin you see in the editor is probably actually an object in Canvas (try expanding that), rather than the gameObject you selected. – Camile Nov 13 '19 at 09:30

1 Answers1

2

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
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Hi Thank you , I am able to see something now in the Globe but not my desired icon pin a Red background as shown in the image https://pasteboard.co/IGu1m46.png, But How can I drag the image to my desired icon image, in your example you are showing a box ,for me its a background when I try to change I do not have option to select it as box, in your screen shot there is box for the sprite that can be seen in the Sprite Renderer https://pasteboard.co/IGu0TiX.png – Jerry Abraham Nov 13 '19 at 10:41
  • how will I ref the prefab to the code in the script ,I created a Empty GameObject from hierarchy window and name it as gameObjectPinPrefab and dragged to the asset folder and clicked 'gameObjectPinPrefab ' from asset folder and added the component "Sprite Renderer" as shown in image https://pasteboard.co/IGupodu.png Now How will I refer this gameObjectPinPrefab to the code? I am a total beginner to Unity thats why this question – Jerry Abraham Nov 13 '19 at 11:41
  • simply drag&drop the according Icon into the `Sprite` field of your prefab (you will first have to change the [Texture's `TextureType`](https://docs.unity3d.com/Manual/class-TextureImporter.html) to `Sprite (2D and UI)`). Then exit the prefab edit mode and drag&drop the prefab it into the according `gameObjectPinPrefab` field in the components Inspector – derHugo Nov 13 '19 at 11:41
  • Finally I am able to add the image/icon after going through this post (https://answers.unity.com/questions/1545197/cannot-drag-drop-png-into-sprite-renderer.html ) as I could not drag the image to the Sprite field of myprefab. Now the is as shown here :https://pasteboard.co/IGuGWkF.png , but how can I can remove the white background of the icon ?do i need to play with the image or anything else to be done? – Jerry Abraham Nov 13 '19 at 12:25
  • I am able to add the icon , but the icon clarity is not good even though I tried different images this is the final which I tried https://pasteboard.co/IGuOm6y.png – Jerry Abraham Nov 13 '19 at 12:41
  • For the transparent Background: You will need to open the image-file in an image editor (Photoshop etc) and make the background transparent. Also if this image is to small in general Unity can not magically make the quality better .. try to use a sprite with a higher resolution. Also check the `Edit` -> `ProjectSettings` -> `Quality Settings` – derHugo Nov 13 '19 at 13:09
  • Hai how can I add text to this prefab Object ?and add onclick event to this object – Jerry Abraham Nov 14 '19 at 05:37
  • @JerryAbraham this is clearly out of scope of your question. Also it is a request for a tutorial/tool which is off-topic for this community. It depends a lot on what your final goal is here. You could use `Image`, `Text` and `Button` components but then the prefab has to spawned as child of a World-Space Canvas. You could also use `SpriteRenderer`, `TextMesh` and make your own button with Collider and `IPointerClickHandler` .. but this goes clearly to broad here :) – derHugo Nov 14 '19 at 05:41