Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Prefab created at runtime is missing sprite reference? (original object is not)

Discussion in 'Scripting' started by Deleted User, Dec 29, 2013.

  1. Deleted User

    Deleted User

    Guest

    I've started work on a very simple tile importer and have hit a bit of a snag.

    I can easily create the sprites and the game objects, add sprite rendere to the game object and connect the sprite to the sprite rendere. But when I create a prefab from the game object, the reference to the sprite is lost?

    Do I need to store the created sprites in some way? Or are there some parameters I must provide when creating the sprites?

    Here's what I got so far.

    Code (csharp):
    1. public class PrefabTest : MonoBehaviour
    2. {
    3.     public Texture2D TilesTexture;
    4.  
    5.     private float TILEWIDTH = 64f;
    6.     private float TILEHEIGHT = 64f;
    7.  
    8.     private int FRAMES_X = 16;
    9.     private int FRAMES_Y = 16;
    10.  
    11.     private List<Sprite> spriteList;
    12.  
    13.     void Awake()
    14.     {
    15.         spriteList = new List<Sprite>();
    16.         Vector2 pv = new Vector2(0f, 0f);
    17.         for (int y = 0; y < FRAMES_Y; y++)
    18.         {
    19.             for (int x = 0; x < FRAMES_X; x++)
    20.             {
    21.                 Rect loc = new Rect(x * TILEWIDTH, (FRAMES_Y - (y + 1)) * TILEHEIGHT, TILEWIDTH, TILEHEIGHT);
    22.                 Sprite s = Sprite.Create(TilesTexture, loc, pv, 64f);
    23.                 s.name = "sprite" + (x + y * FRAMES_Y).ToString("d4");
    24.                 spriteList.Add(s);
    25.             }
    26.         }
    27.     }
    28.  
    29.     // Use this for initialization
    30.     void Start()
    31.     {
    32.         for (int y = 0; y < FRAMES_Y; y++)
    33.         {
    34.             for (int x = 0; x < FRAMES_X; x++)
    35.             {
    36.                 GameObject newTile = new GameObject();
    37.                 SpriteRenderer spriteRenderer = (SpriteRenderer)newTile.AddComponent<SpriteRenderer>();
    38.                 spriteRenderer.sprite = spriteList[x + y * FRAMES_Y];
    39.                 newTile.transform.position = new Vector3(x, -y, 0f);
    40.                 newTile.name = "Tile" + (x + y * FRAMES_Y).ToString("d4");
    41.                 PrefabUtility.CreatePrefab("Assets/Temporary/" + newTile.gameObject.name + ".prefab", newTile, ReplacePrefabOptions.ConnectToPrefab);
    42.             }
    43.         }
    44.     }
    45. }
    All the created game objects (not the prefabs) are shown perfectly..

    Any help would be appreciated.

    - Henning
     
  2. Deleted User

    Deleted User

    Guest

    Solved.. The created sprites does not exist when the script exits. So by loading the sprites via Resources.LoadAll<Sprite>(name) the sprite objects exists, and to instantiate the prefabs from script, I found that I had to use PrefabUtility.InstantiatePrefab instead of the normal Instantiate function.

    After a lot of hours searching the internet and trying out IRC #Unity3d, without success, I finally found the right combinations to make it work. I'll post the full script on http://incd021.com in the near future.

    - Henning
     
    gwelkind likes this.
  3. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    Congrats on fixing it! And thanks for coming back to post the answer you found. :)
     
  4. TobiUll

    TobiUll

    Joined:
    Feb 22, 2015
    Posts:
    73
    Hmmm... I have not been able to find the script on http://incd021.com.
    On which page is it, please?
     
  5. nathanfriend

    nathanfriend

    Joined:
    Mar 26, 2017
    Posts:
    2
    Hi Henning - I am running into the exact issue you described, but I also am having trouble finding the script on http://incd021.com. Can you provide a direct link? It would be much appreciated!
     
  6. nathanfriend

    nathanfriend

    Joined:
    Mar 26, 2017
    Posts:
    2