How to instantiate various prefabs above a sprite and center them.
Hello.
I am trying to instantiate some heart prefabs for an enemy character, one after the other. For the moment being, the system works so that via the edit mode, any user can choose how many prefabs to assign the enemy.
This it how it looks like
As you can see, the hearts prefabs are instantiated, but they are not centered above my character.
This is the current code ton instantiate them:
(the function is called in the Start Method of my c# script, and the variable "SpriteSizeInY" is also created in the start method, and is calculated via GetComponent<SpriteRenderer>().bounds.size.y;
):
[SerializeField] numberOfHearts;
[SerializeField] GameObject heartObject;
GameObject[] ArrayOfHearts;
private Heart heartReference;
void CreateHearts()
{
float firstPos = -0.5f;
ArrayOfHearts = new GameObject[numberOfHearts];
for (int i = 0; i < numberOfHearts; i++)
{
firstPos += 0.2f;
GameObject newHeart = Instantiate(heartObject, transform.position + new Vector3((firstPos), SpriteSizeInY /1.5f, 0), transform.rotation, transform) as GameObject;
ArrayOfHearts[i] = newHeart;
}
}
Is there a Unity function to center them? I am new to Unity and I would be really grateful if someone could help me. Thanks in advance!
Your answer
![](https://koobas.hobune.stream/wayback/20220613042305im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Want to instantiate Prefab in its parent. 1 Answer
Instantiate prefab with its component c# 0 Answers
Changing rotation of prefab 0 Answers
Saving Instantiated objects,Saving Instantiated objects 0 Answers