Spawning prefabs dependant upon Health UI?

Hi. I’m very new to Unity, and am having real trouble learning how to spawn prefabs. I’ve read lots of solutions and watched youtube clips but none of the scripts seem to fit my solution, so any help would be very appreciated!

I have created a 2D runner game in which I need to spawn two prefabs, each dependant upon the Health of the main player at the time. If the health is low, sugar must be spawned more regularly. If the health is high, the Insulin prefab must be spawned more regularly. I need them to come in from the right of the Main Camera.

Obviously, the script below is far from complete. My question is this: what do I do next? Or if I need to rewrite the whole thing, are there any suggestions as to how to write it?

public class SugarClone : MonoBehaviour
{
    Health PlayerHealth;
    public Transform[] spawnLocations;
    public GameObject[] whattospawnPrefab;
    public GameObject[] whattospawnClone;

//Update is called once per frame
public void Awake()
{
	//Find out health of player from alternative script.

	GameObject.FindWithTag("Main_Girl_0");
	PlayerHealth = GetComponent<Health>();
} 

 void Start()
{
    if (PlayerHealth.currentHealth < 5.0)
    {
        spawnSomethingPlease();
    }
}

void spawnSomethingPlease()
{
    whattospawnClone[0] = Instantiate(????);

}

}

Hello there.

You are asking for 2 things:

How to Spawn?

How to decide what to spawn?

For spawining, use Instntiate function. First learn to use it, try to spawn things, somewhere, then learn to spwn them at your desired position.

Then learn tomake some if sentences to decide what to spawn.

After reading your code, i see you maybe need sspend next 3-4 hours learning the basics of Unity functions. Dont try to go fast, go step by step, try to understand all you can, then try try and try,.

You are doing this:

void Start()
 {
     if (PlayerHealth.currentHealth < 5.0)
     {
         spawnSomethingPlease();
     }
 }

So, this part of the code, will be executed only ONCE, the first frame once the object is created. As i see this script is called Sugar, iu understand this script is attached to the sugars you pretend to instantiate, so… this will be executed OONLY when the sugar is instantiated in the scene…

I recoomend you to Learn about Start, Update, Instantiate, and how and when the codes are executed.

Good luck!

Is the health is changing over time?

Then use the Update Method with the Code of the Start method to check PlayerHealth.currentHealth < 5.0 once per frame.

To instantiate use:
Instantiate(prefab, new Vector3(what x, y, z coordinates you want the prefab to spawn), Quaternion.Euler(0, 0, 0));

Quaternion.Euler(0, 0, 0) sets the prefabs rotation to 0,0,0.

Depending on the amount of prefabs you want to spawn, I would recommend pooling (increases performance).