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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Setzer22 · May 31, 2012 at 03:36 PM · instance

About refferencing other scripts

Hello everyone.

I've been looking at some examples of C# in unity, and in some of them I see people refferencing to other scripts like this:

 public Static ScriptName Instance;
 public string randomString = "potatoes";
 
 void Awake () {
    Instance = this;
 }

And then, on another script

 someOtherRandomString = ScriptName.Instance.randomString;

Then the variable someOtherRandomString's value would be "potatoes" as well, so we are refferencing to another script, by creating a variable which holds an actual instance to the first script.

I find this way to be way more clean and easy than using the refferencing through the inspector (because we don't always want the user to be able to configure those refferences from the inspector view), but I wanted to ask: Is this more CPU consuming than the other methods described in the documentation? Is there any way to do this in javaScript (I'm assuming there's one, just don't know it). And lastly I wanted to ask: why isn't this method described in the unity documentation (correct me if i'm wrong but i didn't see it the last time I checked)

And also, I would like to ask: If I use this method, I'm assuming there's only one Instance of that script's class running at the same time. Otherwise the variable Instance would hold two instances of the class at the same time, which can't be as far as I know. Am I right?

Thank you

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

Answer by whydoidoit · May 31, 2012 at 03:53 PM

So that is a singleton pattern and is useful when you will only need to access the one current instance of another class.

The reason to do this rather than just have a totally static class is that you can set the variables on the Instance using the inspector but stil access them from elsewhere without needing to drag a reference.

You may also see script that looks like this:

  public static List<ScriptName> scriptNames = new List<ScriptName>();

  void Awake() {
       scriptNames.Add(this);
 
  }

  void OnDestroy() {
       scriptNames.Remove(this);
 }

This code lets you quickly get all of the instances of the script currently alive in a foreach loop like this (in some other class)

  foreach(var s in ScriptName.scriptNames)
  {
  }

That is MUCH faster than using Find and GetComponent to get all of them.

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 whydoidoit · May 31, 2012 at 03:55 PM 1
Share

Sometimes people use OnEnable and OnDisable rather than Awake and OnDestroy - it depends what you want.

avatar image Setzer22 · May 31, 2012 at 04:39 PM 0
Share

Thank you, that was really useful ^^

avatar image
1

Answer by zico_mahilary · May 31, 2012 at 03:56 PM

in java it would be:

script1.js:

 .....
 static var somevariable = 1;
 .....

script2.js:

 .....
 anothervariable = script1.somevariable; 
 .....

then the value of anothervariable would be 1.

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 Setzer22 · May 31, 2012 at 04:40 PM 0
Share

Thank you. I knew how to use static variables in JavaScript, I just thought this was different from what I knew.

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

Apply changes on asset to instance 1 Answer

Player not moving due to static instance : is dynamic instancing a thing ? 1 Answer

How can I modify a variable on an instance of a prefab after I created it? 2 Answers

Create an Instance? 2 Answers

Unity importing Instances wrong via FBX. 4 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