Instantiated Prefabs Not running start function?

With the following code in place ammoSuccess should always eventually result in true and ammoType should = an ammoType from the gunSlots array, and it does…when its not instatiated, but placed in the scene in the editor, what is the cause of this?

var player : Collider;

var gun;

var guns;

var ammoType : int = 1;

var ammoAmount : int = 25;

var pickupSound : AudioClip;

var switcher : GameObject;

var ammoSuccess : boolean = false;

function Start () 
{
	ammoType = Mathf.Floor(Random.Range(1,10));
	guns = switcher.GetComponent(GunSwitcherScript).gunSlots;
	while (!ammoSuccess)
		{
			for (var b : GameObject in guns)
			{
				if(b.GetComponent(GunStats).ammoType == ammoType)
				{
				ammoSuccess = true;
				}
			}
			if (!ammoSuccess)
			ammoType = Mathf.Floor(Random.Range(1,10));
			
		}
}

Actually, When you Instantiate an Object, it calls Awake() on all components on that object. Sorry, I’m a c# guy :stuck_out_tongue:

Start() get run on the Instantiate object just before it receives its First Update()

I have this script(s) to prove it:

public class Instantiater : MonoBehaviour {
	
	public GameObject copier;
	
	void Start () {
		GameObject GO = (GameObject)Instantiate(copier);
		Instantiatee instantiated = GO.GetComponent<Instantiatee>();
		Debug.Log(instantiated.isAwake);
		Debug.Log(instantiated.isStarted);
		
	}
	
}


public class Instantiatee : MonoBehaviour {

	public bool isStarted = false;
	public bool isAwake = false;
	
	void Awake(){
		isAwake = true;
	}
	
	void Start () {
		isStarted = true;
	}
	
}

I use Awake() for initialize things. I experienced strange things with Start() as well.

You need to make sure your “switcher” game object is defined when this script is instantiated.

If it’s not instantiated and you have it specified in the inspector, everything should work just fine. But if you instantiate it and it’s not defined by default in the prefab/object you’re instantiating… then it shouldn’t work.

The start function always runs, but your member objects might not be defined when the prefab is.