The prefab script is never executed.

What I did:

  1. Created a GameObject
  2. Created a prefab
  3. Dragged GameObject over the prefab
  4. Deleted the GameObject from Scene
  5. 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)

    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));


1 Answer 1


A script is only run when it's in the scene. So any scripts on a prefab will only run after the prefab is instantiated. That of course means you have to instantiate the prefab from some other script, not from within its own script.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .