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 smithy166 · Dec 22, 2013 at 03:51 AM · c#functionvariables

How do I call defined variables within a function? (CS0103)

Hi All,

After bashing my head against a brick wall - and a lot of googling - I've decided it's time I swallow my pride and ask for help!!

I've written some code which will increase/decrease a variable if it's different from the "requested" variable, essentially like the accelerator on a car.

I wrote a function to do this - however, it relies on variables which are defined further up in the script. For whatever reason, I can't call this variables in my function - as unity/mono things they don't exist! (Which they probably don't..)

Code is written in C#

 float RPMportOUT = 0.0f;
 float total = 0.0f;
 public float RPMPGAINP = 0.0f;
 public int portRPMACT = 0;
 float REQRPMP = 0.0f;
 public Telegraph tele;
     
     // Use this for initialization
     void Start () {    
     tele = GameObject.Find("MainShip").GetComponent<Telegraph>();
 }
     
     // Update is called once per frame
     void Update () {
 }
     void fixedupdate ()
     {
 }
 
     void engineRPM()
     {
         if (portRPMACT = REQRPM)
         {
             total = portRPMACT;
         }
         else {
             if (portRPMACT < REQRPM)
             {
                 total = portRPMACT + RPMGAINP;
             } 
         else {
             if (portRPMACT > REQRPM)
             {
                 total = portRPMACT - RPMGAINP;
             }
             return RPMportOUT = total;
             }
         }
     }
 }


Thanks in advance!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by KellyThomas · Dec 22, 2013 at 04:05 AM

 total = portRPMACT; 

total is not declared in the posted code?

 return RPMportOUT = total;

but the function is marked void

Comment
Add comment · Show 2 · 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 smithy166 · Dec 22, 2013 at 01:49 PM 0
Share

Oops! Now I know why you shouldn't write code or post a question at half three in the morning!

What should I mark my function as? Float?

I've edited the code to include the deceleration for "total"

avatar image KellyThomas · Dec 22, 2013 at 02:10 PM 0
Share

If a function has a return type specified then a return value must used for all code paths. In other words a value must be returned regardless of which code paths your condition statements result in.

Your current code returns a value only under these circumstances

 void engineRP$$anonymous$$() {
     if (portRP$$anonymous$$ACT = REQRP$$anonymous$$) { //if this is false (note this should almost certainly be a `==` equality sign!)
         total = portRP$$anonymous$$ACT;
     }
     else {
         if (portRP$$anonymous$$ACT < REQRP$$anonymous$$) {  // and this is false
             total = portRP$$anonymous$$ACT + RP$$anonymous$$GAINP;
         } 
         else {
             if (portRP$$anonymous$$ACT > REQRP$$anonymous$$) {
                 total = portRP$$anonymous$$ACT - RP$$anonymous$$GAINP;
             }
             return RP$$anonymous$$portOUT = total;  // then this will be called
         }
     }
 }

You may find that this behaves in the manner you expect:

 float engineRP$$anonymous$$() {
     float total;
     if (portRP$$anonymous$$ACT == REQRP$$anonymous$$) {
         total = portRP$$anonymous$$ACT;
     }
     else if (portRP$$anonymous$$ACT < REQRP$$anonymous$$) {
         total = portRP$$anonymous$$ACT + RP$$anonymous$$GAINP;
     } 
     else {
         total = portRP$$anonymous$$ACT - RP$$anonymous$$GAINP;
     }
     RP$$anonymous$$portOUT = total;
     return RP$$anonymous$$portOUT;
 }


avatar image
0

Answer by Vonni · Dec 22, 2013 at 01:58 PM

public float ThisFunctionReturnsAFloat () {

  return 2134.0f;

}

fill in the blanks :)

Comment
Add comment · 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

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

20 People are following this question.

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

Related Questions

Function is not updating the variable 1 Answer

is it possible to pass functions through functions? 2 Answers

How do I "trickle" a function's effect over n seconds? 2 Answers

How can you hide variables within a region in a c# script? 1 Answer

How i "import" variables? 2 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