- Home /
Strange increment counter
Hi guys.
I have a variable that is incrementing dependent if the player is holding down the "E" key. It will increment this variable based on a transfer rate. This transfer rate variable is essentially the speed it is incrementing. I am basically passing the transfer rate through as parameter, then incrementing by mulitplying by Time.deltaTime , so that i get an increment every second rather than every frame.
However for some reason when i change the transfer rate variable it doesnt change the speed of the increment. BUT if i replace the variable with an explicit number it works. ....
I dont know why this happens. I have one update method in one of my scripts and the other holds the increment function and keeps the lifeessnce within a range. The transfer rate variable is passed through as parameter.
Script 1
var transferRate:float = 50.0;
// Retrieves the second script attached to this object
var essenceCarrier:Essence_Handler = this.gameObject.GetComponent(Essence_Handler);
function Update {
if(Input.GetKey(KeyCode.E)) {
//EXTRACTING LIFE ESSENCE FROM PLANET INTO CARRIER
essenceCarrier.addEssence(transferRate);
//REDUCE THE ENTITY'S HEALTH
selectedEntityinfo.removeHealth(transferRate);
}
}
Script 2 (Essence_Handler)
var lifeEssence:float = 0;
var minEssence:float = 0;
var maxEssence:float = 100;
function Update () {
lifeEssence = Mathf.Clamp(lifeEssence,minEssence,maxEssence);
}
function addEssence(transferRate:float)
{
lifeEssence += (transferRate*Time.deltaTime);
}
As you can see it passes through to the second script as a parameter, but it doesnt go faster if i change transfer rate in the first script. BUT if i do this in the second script , it does change the speed of the increment of "lifeEssence".
function addEssence(transferRate:float)
{
//EXPLICIT VERSION
lifeEssence += (50*Time.deltaTime);
}
Would really appreciate why this is happening and a solution to this problem, thanks alot.
Well, I'm not sure if this is what is causing your problem, but you are declaring your transferRate variable as a float, but then passing it into your addEssence() function as an int. :/
sorry my bad, i was experimenting whilst doing this question. But yes ive tried it with both float and int. And still doesnt work. Ill edit the question now
Answer by Eric5h5 · Apr 06, 2012 at 05:00 PM
Public variables get their value from the inspector. Changing the variable's value in the script won't do anything (aside from providing the default value for when a script is first attached to an object, or when you use Reset). This allows for quick experimentation, where you can change values in the inspector without having to recompile scripts.
but thats not the problem though, the problem is that ive initially declared the speed to be 50 anyways. yet it doesnt seem to have any effect on the speed of the count. what im essentially saying is that through object referencing the speed doesnt change anything, but if i explicitly say i want it to be 50 it will. The declared 50 through the object reference is not getting picked up, as if it doesnt exist in the code. Yet wen i do this "50*Time.deltaTime" as opposed to "transferRate*Time.deltaTime" where "transferRate" is initialised to 50 on Awake.Its a completely different effect..... where you should expect it to have the same effect.
It should work! Try to set the transferRate directly in the function call:
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.E)) { essenceCarrier.addEssence(70.0); }NOTE: As @Eric5h5 said, the Inspector has an elephant's memory: you change the variable in the script, but the Inspector ignores the new value and keep the older one. You should modify transferRate in the Inspector to see the effect.
ok, i see it now. Is there really any point in decalaring and initialising vaariables like i showed previously because it doesnt get read at runtime? What ive done now is initialised the transfer rate in the Start() method. Declared it outside start() and initialised it to 50 inside the start(). sorry if im asking too many questions, just wanna understand what im doing wrong and why.. Thanks a bunch guys :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613070008im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to build & populate an array available to all scripts on all objects? 1 Answer
How do I change the text of a GUIText object through another GameObject using a variable? 2 Answers
Little image next to GUI ? 1 Answer
four simultaneous touch inputs in android 2 Answers
Boolean and input relation 0 Answers