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 sgmongo · Aug 22, 2013 at 04:28 PM · javascriptfunction

Expanding functions

Alright, this is going to be a very nooby question. I've made a search with google and unity answers but, there is a fundamental terminology that I am missing to make my search successful.

I remember back in highschool learning VB and there being a way to add user-pre-written functions into the code simply by setting them up ahead of time, and including #myextraFunctions at the top of the code.

At the time I used it to include a search and replace command for string data to prove I understood the concept to my teacher.

Here I am more than a decade later learning Javascript in conjunction with unity and have been racking my brain to remember what that was called so I can find a JS~Monodevelop equivalent. As I understand unity I could use getcomponent to simulate this but its heavy on resources.

Would someone be willing to enlighten me once more, and send my a link to a tutorial on how to accomplish this with JS in Monodevelop?

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
3
Best Answer

Answer by Eric5h5 · Aug 22, 2013 at 04:39 PM

Perhaps you're thinking of static functions:

 class Stuff {
     static function Add (a : int, b : int) : int {
         return a + b;
     }
 }

Which you would use like:

 function Start () {
     var x = Stuff.Add(1, 2);
 }

The script with the "Stuff" class doesn't have to be attached to anything, since it's a static function, basically meaning that only one instance can exist.

Comment
Add comment · Show 4 · 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 sgmongo · Aug 22, 2013 at 05:23 PM 0
Share

So you are saying that the first block of code doesn't have to exist in the same script as the second block of code?

If so, how does other scripts find the static function?

Also, maybe something to do with Import, I had to use:

 import UnityEngine;
 import System.Collections;
 import System.Collections.Generic;
 import System.Text;
 import System.Xml;
 import System.IO;

in order to get some X$$anonymous$$L stuff working... maybe I need to make a import file with all my extras...

avatar image Eric5h5 · Aug 22, 2013 at 06:24 PM 2
Share

how does other scripts find the static function?

The scripts find the static function because it's the only one that exists. You identify it with the class name as I showed in the Start code.

maybe I need to make a import file with all my extras...

No. The "import" keyword is slightly misleading, since it doesn't "load" or "import" anything, aside from the namespace. You did not have to use it to get the X$$anonymous$$L stuff working, it just makes things more convenient. For example, ins$$anonymous$$d of doing things the long way:

 var foo = new System.Collections.Generic.List.< int >();

You can import the System.Collections.Generic namespace and do

 var foo = new List.< int >();

While this is nice, it can occasionally result in ambiguity, since the same class may exist in more than one namespace, such as Random existing in UnityEngine.Random and System.Random. So if you do "import System;" and try to use code like "var x = Random.value;" it won't work since the compiler doesn't know which Random you're talking about. If you import the System namespace, you'd have to disambiguate by saying "var x = UnityEngine.Random.value;".

avatar image sgmongo · Aug 22, 2013 at 06:59 PM 0
Share

I will have to try out the static function before I can definitely Check you as the correct answer but, everything you've said so far is simple, easy to understand, and brilliant. Up-votes inco$$anonymous$$g!

$$anonymous$$eep you posted, probably tomorrow.

avatar image sgmongo · Aug 22, 2013 at 07:50 PM 0
Share

This is exactly what I needed. Thanks once again! Self $$anonymous$$ching this stuff is fun but, I miss very important things along the way. Regardless...

You rock!

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

16 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

Related Questions

Source code for library function "RecalculateNormals()"? 4 Answers

Array Problem - Error Code BCE0022 1 Answer

Help with function code, passing Vector3 info 1 Answer

Help with a simple function. 1 Answer

function error 1 Answer


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