Reputation: 173
I am trying to figure out how I can replicate a Start() method inside of another method, with respect to how start definitions are constant throughout instead of updating (For example, if I record a variable called StartTime inside Start() and set it to Time.time). Would a constant integer created inside a method and set to the value of another variable remain the same? For example, here is my code:
const int originalAmount = data.amount;
and later, data.amount is lowered/raised. Will original amount be equal to when it was first set, or equal to the new data.amount value, and if it is equal to the new value is there another way for me to define a variable equal to another variable at time of definition? Thanks for any help you can give me, just having a bit of trouble understanding this!
EDIT: Also, this int is being defined inside a for loop, and the originalAmount should be updated to the current data.amount on each loop, is that correct?
Upvotes: 0
Views: 4317
Reputation: 422
There are few rules associated with constant variable.
However if you are not sure about the value of constant at the time of declaration then you can use readonly.
As constant variable has to be initialize at declaration and you can't initialize it with a variable because constant variable are initialize at compile time. So you can write a constant variable inside a for loop but it will be assigned value at the compile time only.
Upvotes: 4