Hi,
I am working on a 3d Project template, by following this tutorial: How to make Fruit Ninja in Unity (Complete Tutorial) ππͺ - YouTube
Attached the c# script to the game object Spawner. However when I play, nothing happens.
This is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
private Collider spawnArea;
public GameObject[] fruitPrefabs;
public float minSpawnDelay = 0.25f;
public float maxSpawnDelay = 1f;
public float minAngle = -15f;
public float maxAngle = 15f;
public float minForce = 18f;
public float maxForce = 22f;
public float maxLifetime = 5f;
private void Awake()
{
spawnArea = GetComponent<Collider>();
}
private void onEnable()
{
StartCoroutine(Spawn());
}
private void onDisable()
{
StopAllCoroutines();
}
private IEnumerator Spawn()
{
yield return new WaitForSeconds(2f);
while (enabled)
{
GameObject prefab = fruitPrefabs[Random.Range(0, fruitPrefabs.Length)]; //to pick a random fruit from the fruits array
Vector3 position = new Vector3(); //to set a position to spawn the fruit at
position.x = Random.Range(spawnArea.bounds.min.x,spawnArea.bounds.max.x);
position.y = Random.Range(spawnArea.bounds.min.y,spawnArea.bounds.max.y);
position.z = Random.Range(spawnArea.bounds.min.z,spawnArea.bounds.max.z); //these will be random points within the bounds of the spawn collider defined above
//for rotation of the fruits
Quaternion rotation = Quaternion.Euler(0f,0f,Random.Range(minAngle,maxAngle)); //to override the z axis and allow the fruit to rotate along the z axis
GameObject fruit = Instantiate(prefab,position,rotation);
Destroy(fruit,maxLifetime);//built in unity function to destroy the fruit after it reaches the mx lifetime
float force = Random.Range(minForce,maxForce);
fruit.GetComponent<Rigidbody>().AddForce(fruit.transform.up * force,ForceMode.Impulse); //adds an instant force to spawn the fruit up
yield return new WaitForSeconds(Random.Range(minSpawnDelay,maxSpawnDelay));
}
}
}
When I play, the fruits are not spawning.