- Home /
Instanciate prefab responsively
I'm trying to make a simple memory game with cards, so I made a prefab of the card, whith is just a image, how can I instanciate it responsively from the script? depending on the size of the screen it spawns in a diferent location of my UI
this is the part of the code that instanciate my prefab:
for(int c = 0; c<=this.numberOfCards; c++)
{
this.sorteadaValor[c] = Random.Range(0, 13);
this.sorteadaNaipe[c] = Random.Range(0, 4);
prefabCarta.GetComponent<Carta>().setValor(this.sorteadaValor[c]);
prefabCarta.GetComponent<Carta>().setNaipe(this.sorteadaNaipe[c]);
int x = (110 * c) + 185;
Instantiate(prefabCarta, new Vector3(x, 250, 0), Quaternion.identity, GameObject.FindGameObjectWithTag("Canvas").transform);
}
Answer by Pangamini · Oct 24, 2019 at 08:38 AM
What exactly does 'responsively' mean? Are you referring to some layout engine? In that case, you are probably using Unity's RectTransform for layout. In that case, simply setting the card transform's position is not all you can do. Perhaps you should first read about Unity's layout engine. RectTransform is its main component, start from there
Your answer
Follow this Question
Related Questions
How to change Image on button click in a prefab that is instantiated 1 Answer
How to manipulate a variable of a prefab script (instantiated) while the game is runnning . 1 Answer
Canvas, instantiate and adjust size of image to fit parent 0 Answers
How to animate UI rect transform which works for all resolutions 1 Answer
Showing UI image when inside a trigger and then pressing a button 2 Answers