Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by naqvir · Apr 06, 2012 at 03:06 PM · gameobjectinputcounterincrement

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kleptomaniac · Apr 06, 2012 at 03:16 PM 0
Share

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. :/

avatar image naqvir · Apr 06, 2012 at 03:19 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image naqvir · Apr 06, 2012 at 05:17 PM 0
Share

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.

avatar image aldonaletto · Apr 06, 2012 at 05:55 PM 1
Share

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.
avatar image naqvir · Apr 06, 2012 at 06:11 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges