Problem:
The prefab script is never executed.
What I did:
- Created a GameObject
- Created a prefab
- Dragged GameObject over the prefab
- Deleted the GameObject from Scene
- Added the prefab script to prefab.
The prefab is never instantiated and neither is this script executed. What have I missed? Here is the script:
using UnityEngine;
using System.Collections;
public class Fuel : MonoBehaviour
{
GameObject r;
float rmin;
float rmax;
float lastposition;
private BoxCollider box;
bool spawning;
float timer;
Vector3 initial;
//public Vector3[] positions;
void Start()
{
r = GameObject.Find ("Road1");
box = GetComponent<BoxCollider> ();
rmin = r.renderer.bounds.min.x;
rmax = r.renderer.bounds.max.x;
}
void Update()
{
if (!spawning)
{
timer += Time.deltaTime;
}
if (timer >= 2)
{
StartCoroutine(Spawn());
}
}
IEnumerator Spawn()
{
spawning = true;
timer = 0;
initial = new Vector3 (Random.Range(rmin, rmax), -0.23f, Random.Range(r.renderer.bounds.min.z, (r.renderer.bounds.max.z + box.size.z)));
Instantiate (this.gameObject, initial, Quaternion.identity);
yield return new WaitForSeconds (Random.Range (1, 3));
}
}