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

A newly instantiated prefab does not change its size inside Canvas

Discussion in 'Getting Started' started by Kirill_1984, Aug 16, 2021.

  1. Kirill_1984

    Kirill_1984

    Joined:
    Jun 21, 2021
    Posts:
    21
    Hello. I have made a Tetris game. A spawner from which a random tetromino appears has a C# script attached to it (see the script below):

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class SpawnTetromino : MonoBehaviour
    {
    public GameObject[] Tetrominoes;
    private static bool gameStart = false;

    // Start is called before the first frame update
    void Start()
    {
    Randomtetromino();
    }

    // Update is called once per frame
    public void Randomtetromino()
    {
    if (gameStart == false) {
    Instantiate(Tetrominoes[Random.Range(0, Tetrominoes.Length)], transform.position, Quaternion.identity);
    } else {
    Instantiate(Tetrominoes[NextMino.rand], transform.position, Quaternion.identity);
    }
    gameStart = true;
    }
    }


    I am planning to place a whole thing on a canvas so that to position it properly on the screen for each screen resolution. Actually I know how to make it inside Canvas (UI Scale Mode is set to Scale With Screen Size). The only problem I have is that figures that randomly appear on the grid (see the script above) do not change their size (reference resolution always stays the same) according to screen size. So everything scales (grid, text and other panels) except for these prefabs. They look bigger on different screens. If anyone knows how to solve this problem please tell my how I can fix it. It's the only problem I have to finish the game.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You're not providing a parent transform to Instantiate. So they are being created at the root level of your hierarchy, not within the Canvas at all.
     
  3. Kirill_1984

    Kirill_1984

    Joined:
    Jun 21, 2021
    Posts:
    21
    JoeStrout, thank you very much for your reply. Yes, I already figured that out. I did like this:

    public void Randomtetromino()
    {
    GameObject canvas = GameObject.Find("Canvas");
    if (gameStart == false) {
    GameObject mino = Tetrominoes[Random.Range(0, Tetrominoes.Length)];
    GameObject child = Instantiate(mino, transform.position, Quaternion.identity);
    child.transform.SetParent(canvas.transform);
    } else {
    GameObject mino = Tetrominoes[NextMino.rand];
    GameObject child = Instantiate(mino, transform.position, Quaternion.identity);
    child.transform.SetParent(canvas.transform);
    }
    gameStart = true;
    }


    And in the end I got that my tetrominos scaled, but something happened to my grid. Figures are smaller depending on screen size and almost fit, but somehow they lose their precise positions a bit while moving over the gridlines. Something else should be corrected, but I don't know what. If I leave the grid and other stuff beyond canvas it looks absolutely precise, but beyond its bounds after changing resolution. What if I scale them without using UI Scale Mode set to Scale With Screen Size? Is this possible? Can I scale just pixels wihout using this mode? I mean if I use Constant Pixel Size?