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
1
Question by Polraudio · Apr 27, 2015 at 11:13 PM · functionsglobal variables

Pass Function Values To Global Variables

Im trying to make it so when i call the function Mining(GV.IronOre, GV.IronOreExp); it will pass those variables to the function and increase those static variables. Right now i have about 10 if's checking if the name is = to something and thats annoying. Hopefully i can get some help with this. If you need more info just ask but tbh im not sure how else to explain it.

Note: GV is the name of the script where the global variables are stored.

 function Start(){
     Mining(GV.IronOre, GV.IronOreExp);
     Mining(GV.CoalOre, GV.CoalOreExp);
 }
 
 function Mining(Item, Exp){
     Item += 1;
     Exp += 1;
 }
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

1 Reply

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

Answer by Bunny83 · Apr 28, 2015 at 12:08 AM

You showed very little of your actual code. As @dota already said a ref parameter might be a solution but wouldn't be a nice one. Since you use UnityScript you can't even use it so you have to use something else.

First of all static variables have the big problem that you can't reference the instance that holds the variables since static things are instance-free. Furthermore it seems a bit strange to have seperate variables for different ore types hardcoded. You usually would use a class that has an "amount" variable and an "exp" variable. For each ore type you simply create an instance of that class. That way the interaction with an "ore" is always the same. You just need to pass the appropriate reference.

I don't really use UnityScript but it might look like this:

 public class Ore extends System.Object
 {
     var name : String;
     var amount : int;
     var exp : int;
     function Ore(aName : String)
     {
         name = aName;
     }
     function mining()
     {
         amount += 1;
         exp += 1;
     }
 }
 // inside your GV script
 static var iron = new Ore("iron");
 static var coal = new Ore("coal");
 
 // in your other script
 function Start(){
     GV.iron.mining();
     GV.coal.mining();
     // or
     Mining(GV.iron);
 }
 function Mining(ore : Ore) {
     ore.amount += 1:
     ore.exp += 1
 }

Just for simplicity i stayed with the static variables. I would recommend some kind of manager class instead but that would go too far for this answer.

Comment
Add comment · Show 1 · 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 Polraudio · Apr 28, 2015 at 04:46 AM 0
Share

Thanks. You really helped me out with what i was looking for. Was a little more complex than what i wanted and needed a little tweaks but i got it to work. Thank you very much :D

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Animation Event don't show C# methods 1 Answer

run function once per frame 2 Answers

Auto generated script with more functions? 0 Answers

yield WaitForSeconds preventing further actions. 1 Answer

How exactly do Monobehaviour functions work? 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