Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Instantiated child prefab is invisible

Discussion in '2D' started by Maxik2k, Jul 27, 2018.

  1. Maxik2k

    Maxik2k

    Joined:
    Mar 30, 2018
    Posts:
    9
    Hi!
    I have a problem with 2D Unity project. When I instantiate prefabs in my canvas, objects are created, I am able to click on them, but they're invisible in the Scene and Game panels. I tried to change Z-positon, but it still doesn't work. Here is my script, which I use on the parent of the prefabs:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BlockSpawner : MonoBehaviour
    6. {
    7.     public Transform[] spawnPoints;
    8.  
    9.     public GameObject blockPrefab;
    10.  
    11.     public void Start ()
    12.     {
    13.                 Instantiate (blockPrefab, spawnPoints[i].position, Quaternion.identity);
    14.     }
    15. }
    How can I make the prefabs is visible? Thanks for any help.
     
  2. Salade_de_poney

    Salade_de_poney

    Joined:
    Jan 20, 2017
    Posts:
    60
    With 2D components, you also need to check the Order in Layer it is casted to. If it's behind your ground, you won't see it.
    Screen Shot 2018-07-27 at 14.06.10.png
     
  3. Maxik2k

    Maxik2k

    Joined:
    Mar 30, 2018
    Posts:
    9
    The order in layer of the parent prefab is 3 and the child is 5 so that should be OK. I changed the sorting layer of both the parent and child prefab but the child is still invisble. When I drag the prefab into the scene it's displayed correctly. The problem isn't in layers
     
    Last edited: Jul 28, 2018
  4. Salade_de_poney

    Salade_de_poney

    Joined:
    Jan 20, 2017
    Posts:
    60
    Hey sorry for the late reply,

    Did you already solve your problem ?

    If not the problem might lie in the way you instantiate the GameObject. Quaternion.identity will reset all rotations of the parent prefab to zero.
     
  5. Maxik2k

    Maxik2k

    Joined:
    Mar 30, 2018
    Posts:
    9
    Oh, no, I didn't solve this problem.
    Could you tell me, how I should instantiate my prefabs without using Quaternion.identity? If it's possible of course
     
  6. Salade_de_poney

    Salade_de_poney

    Joined:
    Jan 20, 2017
    Posts:
    60
    Of course,

    The easy way is to use Quaternion.Euler(a, b, c) where a, b, c are in Degrees (the angles you put in your inspector).
     
  7. Maxik2k

    Maxik2k

    Joined:
    Mar 30, 2018
    Posts:
    9
    Oh, there are appeared new conditions. So, prefabs are invisible, if they have Image Component. If I use Sprite Renderer instead Image or prefab is a Сube for example - it's visible. But I need to use Image Component. I really don't know what the problem is. So, have you any idea, how to make the Image Component is visible?
     
    Last edited: Aug 2, 2018
  8. Salade_de_poney

    Salade_de_poney

    Joined:
    Jan 20, 2017
    Posts:
    60
    If your Asset's Texture Type is a Sprite(2D and UI) . You can use the "RawImage" component with:

    <code>
    RawImage rawImage;
    Spite yourSprite;

    rawImage.texture = yourSprite.texture;
    </code>

    It also work if you want to display videos. You can't change the "Texture" component of an image as it is "readOnly".

    So if you want to display a Sprite in an Image, use "RawImage".
     
  9. mbin828

    mbin828

    Joined:
    Jan 29, 2019
    Posts:
    1
    I know this is late but I had the same exact problem and it was really frustrating. I'm surprised no one answered this question but this is how I solved it:


    I put the spawn point under a sprite. You can leave the sprite field in the sprite renderer empty and it looks like a regular empty 2d point.


    I was just trying random S*** and have no idea why this works.
     
  10. rxx1

    rxx1

    Joined:
    Feb 6, 2021
    Posts:
    1
    This may be a bit late but I'll put this here for others who come across this thread

    if the z position of the spawn point and the player are not both equal to 0 then it will spawn invisible

    hope this fixes your problem :)
     
    Ledorubik likes this.
  11. Ledorubik

    Ledorubik

    Joined:
    Sep 27, 2014
    Posts:
    1
    Thanks. This worked for me.
     
  12. blakeduvallrobertson

    blakeduvallrobertson

    Joined:
    Oct 14, 2020
    Posts:
    1
    check the z axis
     
unityunity