- Home /
Can anyone tell me why the start function on this script is starting before the object it is attached too.
I was using this code in which this ally object would be set active only if the third hero of the game "tamed" it For heroes 1, 2 and 4 this is the case but for hero 3 this script is called before the taming which causes a lot of problems. Can anyone explain why? I'm at a complete loss as I have 3 other scripts that are near identical except for different variables and they activate at the correct time.
public void ally3()
{
speciesid = GameInformation.Despeciesid;
newEnemy.EnemyClass = new Ally3Class();
newEnemy.EnemyLevel = allylevel;
newEnemy.EnemyName = newEnemy.EnemyClass.EnemyName;
newEnemy.Stamina = newEnemy.EnemyClass.Stamina;
newEnemy.Strength = newEnemy.EnemyClass.Strength;
newEnemy.Endurance = newEnemy.EnemyClass.Endurance;
newEnemy.Intellect = newEnemy.EnemyClass.Intellect;
newEnemy.Resistance = newEnemy.EnemyClass.Resistance;
newEnemy.Agility = newEnemy.EnemyClass.Agility;
newEnemy.Energy = newEnemy.Energy;
newEnemy.SlashResistance = newEnemy.EnemyClass.SlashResistance;
newEnemy.PierceResistance = newEnemy.EnemyClass.PierceResistance;
newEnemy.BluntResistance = newEnemy.EnemyClass.BluntResistance;
newEnemy.BlastResistance = newEnemy.EnemyClass.BlastResistance;
newEnemy.FireResistance = newEnemy.EnemyClass.FireResistance;
newEnemy.WaterResistance = newEnemy.EnemyClass.WaterResistance;
newEnemy.IceResistance = newEnemy.EnemyClass.IceResistance;
newEnemy.ElectricResistance = newEnemy.EnemyClass.ElectricResistance;
newEnemy.WindResistance = newEnemy.EnemyClass.WindResistance;
newEnemy.EarthResistance = newEnemy.EnemyClass.EarthResistance;
newEnemy.LightResistance = newEnemy.EnemyClass.LightResistance;
newEnemy.DarkResistance = newEnemy.EnemyClass.DarkResistance;
newEnemy.BaseEXP = newEnemy.EnemyClass.BaseEXP;
gainedexp = newEnemy.GainedEXP;
newEnemy.BaseSkillEXP = newEnemy.EnemyClass.BaseskillEXP;
newEnemy.BreathlessResistance = newEnemy.EnemyClass.BreathlessResistance;
newEnemy.BurnResistance = newEnemy.EnemyClass.BurnResistance;
newEnemy.ConfusedResistance = newEnemy.EnemyClass.ConfusedResistance;
newEnemy.FreezeResistance = newEnemy.EnemyClass.FreezeResistance;
newEnemy.PoisonResistance = newEnemy.EnemyClass.PoisonResistance;
newEnemy.SleepResistance = newEnemy.EnemyClass.SleepResistance;
newEnemy.SoakResistance = newEnemy.EnemyClass.SoakResistance;
newEnemy.StunResistance = newEnemy.EnemyClass.StunResistance;
newEnemy.ParalysisResistance = newEnemy.EnemyClass.ParalysisResistance;
stamina = newEnemy.Stamina;
energy = newEnemy.Energy;
// converting to base stats
enemyname = newEnemy.EnemyName;
health = (newEnemy.Stamina * 10.0f) * (newEnemy.EnemyLevel / 50.0f) + 20 + newEnemy.EnemyLevel;
health = Mathf.Round(health * 1f) / 1f;
attack = (newEnemy.Strength * 5.0f) * (newEnemy.EnemyLevel / 50.0f) + 20 + newEnemy.EnemyLevel;
attack = Mathf.Round(health * 1f) / 1f;
defense = (newEnemy.Endurance * 5.0f) * (newEnemy.EnemyLevel / 50.0f) + 20 + newEnemy.EnemyLevel;
defense = Mathf.Round(health * 1f) / 1f;
Mattack = (newEnemy.Intellect * 5.0f) * (newEnemy.EnemyLevel / 50.0f) + 20 + newEnemy.EnemyLevel;
Mattack = Mathf.Round(health * 1f) / 1f;
Mdefense = (newEnemy.Resistance * 5.0f) * (newEnemy.EnemyLevel / 50.0f) + 20 + newEnemy.EnemyLevel;
Mdefense = Mathf.Round(health * 1f) / 1f;
speed = (newEnemy.Agility * 5.0f) * (newEnemy.EnemyLevel / 50.0f) + 20 + newEnemy.EnemyLevel;
speed = Mathf.Round(health * 1f) / 1f;
mana = (newEnemy.Energy * 4.0f) * (newEnemy.EnemyLevel / 50.0f) + 20 + newEnemy.EnemyLevel;
mana = Mathf.Round(health * 1f) / 1f;
requiredexp = (2 * (allylevel * allylevel * allylevel) + (allylevel * allylevel) + (8 * allylevel) - 8) / 3;
slashr = newEnemy.SlashResistance;
piercer = newEnemy.PierceResistance;
bluntr = newEnemy.BluntResistance;
blastr = newEnemy.BlastResistance;
firer = newEnemy.FireResistance;
waterr = newEnemy.WaterResistance;
windr = newEnemy.WindResistance;
electricr = newEnemy.ElectricResistance;
icer = newEnemy.IceResistance;
earthr = newEnemy.EarthResistance;
lightr = newEnemy.LightResistance;
darkr = newEnemy.DarkResistance;
breathlessr = newEnemy.BreathlessResistance;
burnr = newEnemy.BurnResistance;
confusedr = newEnemy.ConfusedResistance;
stunr = newEnemy.StunResistance;
freezer = newEnemy.FreezeResistance;
soakr = newEnemy.SoakResistance;
poisonr = newEnemy.PoisonResistance;
sleepr = newEnemy.SleepResistance;
paralysisr = newEnemy.ParalysisResistance;
baseexp = newEnemy.BaseEXP;
baseskillexp = newEnemy.BaseSkillEXP;
cattack = attack;
cdefense = defense;
cMattack = Mattack;
cMdefense = Mdefense;
cspeed = speed;
cslashr = slashr;
cpiercer = piercer;
cbluntr = bluntr;
cblastr = blastr;
cfirer = firer;
cwaterr = waterr;
cwindr = windr;
celectricr = electricr;
cicer = icer;
cearthr = earthr;
clightr = lightr;
cdarkr = darkr;
cbreathlessr = breathlessr;
cburnr = burnr;
cconfusedr = confusedr;
cstunr = stunr;
cfreezer = freezer;
csoakr = soakr;
cpoisonr = poisonr;
csleepr = sleepr;
cparalysisr = paralysisr;
}
private void calccurrentstats()
{
newEnemy.Stamina = newEnemy.EnemyClass.Stamina;
stamina = newEnemy.Stamina;
// converting to current stats
cattack = attack;
cdefense = defense;
cMattack = Mattack;
cMdefense = Mdefense;
cspeed = speed;
cslashr = slashr;
cpiercer = piercer;
cbluntr = bluntr;
cblastr = blastr;
cfirer = firer;
cwaterr = waterr;
cwindr = windr;
celectricr = electricr;
cicer = icer;
cearthr = earthr;
clightr = lightr;
cdarkr = darkr;
cbreathlessr = breathlessr;
cburnr = burnr;
cconfusedr = confusedr;
cstunr = stunr;
cfreezer = freezer;
csoakr = soakr;
cpoisonr = poisonr;
csleepr = sleepr;
cparalysisr = paralysisr;
// Stage Modifiers
if (attackstage == 0)
{
cattack = attack;
cattack = Mathf.Round(cattack * 1f) / 1f;
}
if (attackstage == 1)
{
cattack = attack * 1.5f;
cattack = Mathf.Round(cattack * 1f) / 1f;
}
if (attackstage == 2)
{
cattack = attack * 2;
cattack = Mathf.Round(cattack * 1f) / 1f;
}
if (attackstage == -1)
{
cattack = attack * 0.75f;
cattack = Mathf.Round(cattack * 1f) / 1f;
}
if (attackstage == -2)
{
cattack = attack * 0.5f;
cattack = Mathf.Round(cattack * 1f) / 1f;
}
if (magicalattackstage == 0)
{
cMattack = Mattack;
cMattack = Mathf.Round(cMattack * 1f) / 1f;
}
if (magicalattackstage == 1)
{
cMattack = Mattack * 1.5f;
cMattack = Mathf.Round(cMattack * 1f) / 1f;
}
if (magicalattackstage == 2)
{
cMattack = Mattack * 2;
cMattack = Mathf.Round(cMattack * 1f) / 1f;
}
if (magicalattackstage == -1)
{
cMattack = Mattack * 0.75f;
cMattack = Mathf.Round(cMattack * 1f) / 1f;
}
if (magicalattackstage == -2)
{
cMattack = Mattack * 0.5f;
cMattack = Mathf.Round(cMattack * 1f) / 1f;
}
if (defensestage == 0)
{
cdefense = defense;
cdefense = Mathf.Round(cdefense * 1f) / 1f;
}
if (defensestage == 1)
{
cdefense = defense * 1.5f;
cdefense = Mathf.Round(cdefense * 1f) / 1f;
}
if (defensestage == 2)
{
cdefense = defense * 2;
cdefense = Mathf.Round(cdefense * 1f) / 1f;
}
if (defensestage == -1)
{
cdefense = defense * 0.75f;
cdefense = Mathf.Round(cdefense * 1f) / 1f;
}
if (defensestage == -2)
{
cdefense = defense * 0.5f;
cdefense = Mathf.Round(cdefense * 1f) / 1f;
}
if (magicaldefensestage == 0)
{
cMdefense = Mdefense;
cMdefense = Mathf.Round(cMdefense * 1f) / 1f;
}
if (magicaldefensestage == 1)
{
cMdefense = Mdefense * 1.5f;
cMdefense = Mathf.Round(cMdefense * 1f) / 1f;
}
if (magicaldefensestage == 2)
{
cMdefense = Mdefense * 2;
cMdefense = Mathf.Round(cMdefense * 1f) / 1f;
}
if (magicaldefensestage == -1)
{
cMdefense = Mdefense * 0.75f;
cMdefense = Mathf.Round(cMdefense * 1f) / 1f;
}
if (magicaldefensestage == -2)
{
cMdefense = Mdefense * 0.5f;
cMdefense = Mathf.Round(cMdefense * 1f) / 1f;
}
if (speedstage == 0)
{
cspeed = speed;
cspeed = Mathf.Round(cspeed * 1f) / 1f;
}
if (speedstage == 1)
{
cspeed = speed * 1.5f;
cspeed = Mathf.Round(cspeed * 1f) / 1f;
}
if (speedstage == 2)
{
cspeed = speed * 2;
cspeed = Mathf.Round(cspeed * 1f) / 1f;
}
if (speedstage == -1)
{
cspeed = speed * 0.75f;
cspeed = Mathf.Round(cspeed * 1f) / 1f;
}
if (speedstage == -2)
{
cspeed = speed * 0.5f;
cspeed = Mathf.Round(cspeed * 1f) / 1f;
}
}
// Use this for initialization
void Start()
{
newEnemy = new BaseEnemy();
LoadAlly3Info();
allylevel = GameInformation.DeAllyLevel;
ally3();
Skill1();
saveAlly3infomation();
attackstage = 0;
defensestage = 0;
magicalattackstage = 0;
magicaldefensestage = 0;
speedstage = 0;
Stun = false;
Burn = false;
Frozen = false;
Poison = false;
Breathless = false;
Confused = false;
Sleep = false;
Soak = false;
Paralysis = false;
calccurrentstats();
}
// Update is called once per frame
void Update()
{
calchealthbar();
calccurrentstats();
Statuscheck();
DisplayHPText();
switch (currentstate)
{
case (UnitStates.IDLE):
Defendstatus();
break;
case (UnitStates.UNITSELECTION):
defendstatus = false;
if (ATBCombatStateMachine.previousState == ATBCombatStateMachine.BattleStates.CLASH || ATBCombatStateMachine.previousState == ATBCombatStateMachine.BattleStates.TEAMCLASH)
{
if (ButtonManager.attackaffinity == ButtonManager.AttackAffinities.PHYSICAL)
{
ATBCombatStateMachine.playerclashattack = cattack;
}
if (ButtonManager.attackaffinity == ButtonManager.AttackAffinities.MAGICAL)
{
ATBCombatStateMachine.playerclashattack = cMattack;
}
if (ButtonManager.attackaffinity == ButtonManager.AttackAffinities.HYBRID)
{
ATBCombatStateMachine.playerclashattack = chattack;
}
ATBCombatStateMachine.playerclashlevel = allylevel;
ATBCombatStateMachine.playerclashid = id;
}
else
{
ATBCombatStateMachine.attackerid = id;
ButtonManager.unitid = id;
ATBCombatStateMachine.attackername = enemyname;
ATBCombatStateMachine.attackerlevel = allylevel;
if (ButtonManager.attackaffinity == ButtonManager.AttackAffinities.PHYSICAL)
{
ATBCombatStateMachine.unitattack = cattack;
}
if (ButtonManager.attackaffinity == ButtonManager.AttackAffinities.MAGICAL)
{
ATBCombatStateMachine.unitattack = cMattack;
}
if (ButtonManager.attackaffinity == ButtonManager.AttackAffinities.HYBRID)
{
ATBCombatStateMachine.unitattack = chattack;
}
}
if (speciesid == "SL1")
{
ForestSlimeAI.skilllevel = ss1Lvl;
loyaltycheck();
FSL.Allyforestslimeai();
}
else if (speciesid == "IM1")
{
ForestImpAI.skilllevel = ss1Lvl;
loyaltycheck();
FI.allyforestimpai();
}
currentstate = UnitStates.IDLE;
break;
case (UnitStates.DEAD):
break;
case (UnitStates.REVIVE):
break;
}
}
public void calchealthbar()
{
float CHPBar = chealth / health;
if (chealth > health)
{
CHPBar = 1f;
}
else if (chealth < 0)
{
CHPBar = 0;
}
HPbar.transform.localScale = new Vector3(CHPBar, HPbar.transform.localScale.y, HPbar.transform.localScale.z);
}
void DisplayHPText()
{
ally3HP.text = chealth.ToString();
}
private void saveAlly3infomation()
{
GameInformation.DeAllyLevel = allylevel;
GameInformation.DeAllyHealth = health;
GameInformation.DeAllycurrentHealth = chealth;
GameInformation.DeAllyPhysicalAttack = attack;
GameInformation.DeAllyPhysicalDefense = defense;
GameInformation.DeAllyMagicalAttack = Mattack;
GameInformation.DeAllyMagicalDefense = Mdefense;
GameInformation.DeAllySpeed = speed;
GameInformation.DeAllyHybridAttack = hattack;
GameInformation.DeAllyHybridDefense = hdefense;
GameInformation.DeAllyCurrentEXP = currentexp;
GameInformation.DeAllyRequiredEXP = requiredexp;
GameInformation.DeAllyMana = mana;
GameInformation.DeAllycurrentMana = cmana;
GameInformation.DeAllyRequiredskill1EXP = requireds1exp;
GameInformation.DeAllyCurrentSkill1EXP = currents1exp;
}
private void LoadAlly3Info()
{
allylevel = GameInformation.DeAllyLevel;
ss1Lvl = GameInformation.DeAllyskill1level;
if (allylevel < 1)
{
allylevel = 1;
ally3();
chealth = health;
cmana = mana;
GameInformation.DeAllycurrentMana = cmana;
GameInformation.DeAllycurrentHealth = chealth;
}
chealth = GameInformation.DeAllycurrentHealth;
cmana = GameInformation.DeAllycurrentMana;
currentexp = GameInformation.DeAllyCurrentEXP;
requiredexp = GameInformation.DeAllyRequiredEXP;
currents1exp = GameInformation.DeAllyCurrentSkill1EXP;
requireds1exp = GameInformation.DeAllyRequiredskill1EXP;
}
}
Ain't nobody going to read through such a huge piece of code, Insulate your code to where you think the problem lies.
He's right. Noone will check 1500 lines of code. Condense it on the needed parts...
Wow... $$anonymous$$An..
We will not read this..
YOu need to concentrate the information, and give us a simple basic question to be solved... we will not read and understand more than 1000 lines of code... what do you think we are? gods?
Bye!
Usually I have to ask for a bit more detail from those posting questions, because I often see something about as detailed as "it's broke, how do I fix it".
THIS is from an entirely different universe! :)
$$anonymous$$aybe post what may look like just the pertinent stuff, then zip up the rest as an attached file if you think we need everything.
Answer by JVene · Jul 16, 2018 at 11:22 AM
@uranus10, I'm going on your title and opening description only here.
There is no guarantee of the order in which Start is called for GameObjects. I generally see children are called before more parent objects, but there's no controlled order that I recognize or find documented.
However, there's a designed solution to your problem.
void Awake() {...}
It is not widely used or obvious in documentation or tutorials, but Awake is exactly like Start in that it is called once in the lifetime of all objects. However, it is guaranteed by the Unity framework that all Awake functions will be called before any Start functions, so you can synchronize the prerequisite initialization operations by moving that code which must be executed first, relative to Start code (not relative to any specific objects) by moving that code into an Awake function instead.
Another more generalized solution to this basic issue can be lazy initialization. In this approach you assume some prerequisite state must exist in a parent or supervisory object called upon by subordinate objects. For lazy initialization to work there must be a means of referencing the supervisor from the subordinates (child objects) such that if the supervisor is not suitably initialized, the call from the first child performs the initialization. This is usually done as a static public function in the supervisor used for the child to "find" the parent, which implies the parent is performing the self initialization on the first call from the child, so subsequent children calling in, using the same static public function in the parent, find the parent fully initialized.
That actually fixed the order of the script activation for me. Thank you so much.
Actually, that's new to me. While it can solve this local problem, as a method for dealing with synchronization of code, this is suboptimal. Code should not depend upon settings like this to operate correctly. To that point, there a lots of Unity centric issues with which I disagree, like the inspector setting of initializing values that override the initialization values written in the source code. This promotes brittle solutions, which experience (in other technologies going back decades) shows me should be avoided. Setting parameters in the inspector is a convenience for debugging and construction, but it confuses what the code says vs what the result ends up doing. This does something similar. Personally, now that I know this feature exists, I would choose never to use it. If my own code relied upon this setting to function, I'd recognize that as unreliable and non-transportable. In my view, this is a solution to the local problem, but it leaves the real problem, that of synchronization, in the code.
I don't think Awake works for me in this case. The start function for this and the other 3 activate the moment the object is activated, or atleast that's what was aimed for. In this case its the activeness of the object the script is attached to. That object is not active at the start yet the script activates anyway.
I think I've got an idea now tho, Thanks for the response
When you say "the moment the object is activated", are you $$anonymous$$dful of the fact that Start (and Awake), no matter how arranged, only runs once in the entire lifetime of the object?
Yes, I am aware of that. What I mean is that the start function should only run when the object becomes active. I found my problem was the object became active before the state machine's script ran which caused the start function to run that one time. Now that i've fixed the ordering everything is working as should.
Your answer
![](https://koobas.hobune.stream/wayback/20220612170721im_/https://answers.unity.com/themes/thub/images/avi.jpg)