Question by
LeanMyHeart · Oct 22, 2018 at 06:29 PM ·
instantiate prefab
Instantiate Prefab From Script Inside A Canvas
Hello Im making brick breaker game. I want to load level2 prefab inside a canvas. I found in forum but i can't use to this function in to my code because i didnt use gameobject im using Transform. Please Help :)
Old Forum Code:
GameObject enemy = Instantiate(enemyPrefab, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
enemy.transform.SetParent (GameObject.FindGameObjectWithTag("Canvas").transform, false);
MyCode:
public int numberOfBricks;
public Transform[] Levels;
public int currentLevelIndex = 0;
public void UpdateNumberOfBricks()
{
numberOfBricks--;
if(numberOfBricks <= 0)
if (currentLevelIndex >= Levels.Length - 1)
{
GameOver();
}
else
{
gameOver = true;
Invoke("LoadLevel", 3f);
}
}
void LoadLevel()
{
currentLevelIndex++;
Instantiate(Levels[currentLevelIndex], Vector2.zero, Quaternion.identity);
numberOfBricks = GameObject.FindGameObjectsWithTag("brick").Length;
gameOver = false;
}
Thanks
Comment
Your answer
Follow this Question
Related Questions
Instantiating at a specific location? 0 Answers
How to Instantiate platforms with certain space inbetween each platform (Endless Runner) 0 Answers
turret shoot in direction and hit my health (scripts attached,i need to know how to put it together) 0 Answers
Prefabs not instantiating after build 0 Answers
Why do my first 50 instances of a prefab have the same properties? 1 Answer