Hi i'd like to ask how can i check a value once and depending of the result d something. It looks like that : Code (CSharp): if(value == true) float t += Time.deltatime; But i want to check the value just at begin of the script and later add time to t for rest of time: Code (CSharp): void Start(){ if(value == true) AddTime(); else DoubleTime(); } void AddTime(){ float t += time.deltatime; } void DoubleTime(){ float t = 2* time.deltatime } So how can i start an "void Update" from Start or Awake? If there is a way then is that a better option insted of checking value every frame in Update if value is set before script starts?
Code (CSharp): float multiplier; float time; public bool myBool; //I guess you want this public to show in Inspector? void Start() { multiplier = myBool == true ? 1f : 2f; } void Update() { time += Time.deltaTime * multiplier; }
Ok i have lerned something thank You. And what if a bool is not a bool but enum? What i want to do is to check what type of enemy i have spawned so for example there is enum called warrior, archer,wizard and depending of the enemy class use different void. For now i have done it like that: Code (CSharp): private void Update() { health_bar.transform.position = transform.position; if (ea.typeOfEnemy == Enemy_attributes.EnemyType.malee) { Malee(); } else if (ea.typeOfEnemy == Enemy_attributes.EnemyType.ranged) { Ranged(); } } But im not sure if checking the profession of enemy every frame make sens.
No, it doesn't make sense. You check it once (after you spawned the enemy for example), and based on that , you run your code accordingly. I also notice you are moving the health bar in Update, but your enemies could have an healthbar as child.
So how can i check that typeOfEnemy just once? My health is a circle cuttoff and i dont know ho to do it as gameobject so i put his health into canvas where Image has option Radial.
You can try something like this: Code (CSharp): public class YourClass: MonoBehaviour { /* ... */ System.Action _enemyHandler; private void OnEnable() { _enemyHandler = (ea.typeOfEnemy == Enemy_attributes.EnemyType.malee) ? (System.Action)Malee : Ranged; } private void Update() { health_bar.transform.position = transform.position; _enemyHandler(); } private void Malee() { /* ... */ } private void Ranged() { /* ... */ } }
Oh for some reason i haven't try to put Canvas into an object in hierarchy. So my enemy now have a child which have canvas and image at the same object. Width and height are set at 0.25 and canvas is a square around my enemy and Health circle Is that what you had in mind? //Edit How can i add an picture here?