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 windexglow · Jul 14, 2010 at 04:26 PM · variablespublicprivate

Explanation on how to use functions, variables from other files

I seem to be running deadstraight into wall after wall with trying to figure this out. Searching points me to topics that have either nothing to do with what I want, or ones that I have looked at already.

1 : How do I use variables created outside functions in another file?

2 : What is the difference and limitations of variable types (private, public, ect)

3 : How do I use functions from 1 file in another? What if they are in a different gameobject?

4: What is the difference and limitations of function types (private, public, ect)

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

Answer by Tetrad · Jul 14, 2010 at 04:40 PM

I'm assuming you're using Javascript.

Everything in one file is in one class. private variables and functions can only be used inside that class. public variables and functions can be used by that class and anything that references that class.

All of your scripts are a subtype of Component. So like how you can do var myRigidbody = GetComponent( "Rigidbody"), you can do the same thing with your classes.

Once you have a reference to your custom component type, you can then call functions and access variables just like anything else.

As a basic example:

MyClass.js:

var counter : int = 0;

function DoIncrementCounter() { counter += 1; WillNotBeAbleToCallThisExternally(); }

private function WillNotBeAbleToCallThisExternally() { Debug.Log( "private function" ); }

Notice there's no public declaration since in Javascript everything is assumed to be public by default. In C# it's the opposite, where everything is assumed to be private.

In another script that's attached to the same game object as the above is, you can do this:

var myClass = GetComponent( "MyClass" );
var myClassCounter = myClass.counter;
myClass.DoIncrementCounter();

And that should be obvious what that does. However you won't be able to call the private function, or access private variables with a reference to your class.

If that script is on another game object, first you have to get a reference to the game object that your script is on. From there it's the same, you would just call GetComponent on the game object reference itself. You can search by name (bad idea, but what most people start with since it makes the most obvious sense), or you can set up references inside your script and set things up ahead of time in the scene or via instantiation.

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 windexglow · Jul 14, 2010 at 04:59 PM 0
Share

Thank you, that explained it perfectly for me.

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

No one has followed this question yet.

Related Questions

public private and static variables in js 1 Answer

How faster is private variables over public variables? 1 Answer

Is this the right way to make a variable available to other scripts? 1 Answer

Instantiate an instance (clone) only copies public fields? 1 Answer

Public variables vs {get{return variable;}} 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