is the Start function not a special first called function.
I ask because i have a torpedo object and the torpedo is designed to behave fairly realistic so it starts out sitting there then when fire is pressed it accelerates for 2 seconds towards its target. After 2 seconds it stops accelerating and cruises to target.
however with some debug.log i have discovered that
void Start()
{
bool Burn = false;
}
bool Fire()
{
Burn = true;
}
void FixedUpdate()
{
if(Burn)
{
//accelerate torp
}
}
results in the torps burn value going from false to true to false
it is called in the order
Torp.Fire();
Torp.Start();
Torp.FixedUpdate();
it seems nonsensical to me that this happens especially because i instantiate the object before I fire.
It should result in a
Instantiate (//results in Start called);
Torp.Fire();
I can post my actual code if needed though its longer and more complicated.
anyone explain if this is expected behavior.
I understand that PERHAPS the order on the stack is such that it gets called that way (though i still feel that since the instantiate torp comes before fire torp the start should come before fire)
But is this expected behavior, does unity not make sure start have priority in the que?
I expected this to behave as a class constructor. Is there an actual class constructor?
public GameObject target;
BallisticWeapon Turret;
public GameObject TorpedoPrefab;
GameObject TorpedoObject;
Torpedo TorpedoFunctions; //inherits from ballistic projectile
void Start()
{
Turret = gameObject.GetComponent<BallisticWeapon>();
Turret.ProjectileSpawnPoints = new List<Transform>();
foreach (Transform Child in transform.root)
{
if( Child.tag == "ProjectileSpawnPoint")
{
Turret.ProjectileSpawnPoints.Add(Child);
}
}
Turret.AutoAim = true;
TorpedoObject = Instantiate(TorpedoPrefab,transform.position,transform.rotation) as GameObject;
TorpedoObject.rigidbody.isKinematic = true;
TorpedoObject.transform.parent = transform;
TorpedoFunctions = TorpedoObject.GetComponent<Torpedo>();
Debug.Log(TorpedoFunctions);
Turret.Accuracy = 1;
Turret.projectile = TorpedoFunctions;
Turret.AmmoCount = 12;
Turret.Fire();
}
}
torpedo script
public class Torpedo : BallisticProjectile {
public float Acceleration;
public float BurnTime;
public bool Burn;
float timer;
// Use this for initialization
void Start () {
Debug.Log("burn in start" + Burn);
Burn = false;
timer = 0f;
BurnTime = 2f;
MetersPerSecond = 15f;
}
public override bool Fire(){
Debug.Log("in torp fire");
rigidbody.isKinematic = false;
Burn = true;
Debug.Log(Burn);
return true;
}
// Update is called once per frame
void FixedUpdate () {
Debug.Log(Burn);
if(Burn){
rigidbody.AddForce(transform.forward * MetersPerSecond);
timer += Time.fixedDeltaTime;
if(Mathf.Approximately(timer,BurnTime) || timer > BurnTime){
Burn = false;
}
}
}
}