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 XirohpueLynx · Aug 20, 2020 at 11:07 PM · script.textreferenceaccessdefinition

Is constantly referencing or defining once then changing better

Hi I'm relatively new to C#/Unity.

I have two scripts which reference each other and I need to regularly access a somewhat constant from the "main" script (the variable's value stays mostly the same, it gets increased/decreased extremely infrequently). It is used pretty frequently in the second script. From what I see, people often advice against using GetComponent, but I've used it anyways. I'm just wondering if it would be better to define a variable in the second script using the "main" script's variable value so unity won't constantly need to go and refer back to main and take the value out of there each "update" and change it when needed with a function. Or would just using main.variable be more optimal.

For example,

 //**mainScript**
 x=3;
 
 increaseX():
  - increase x
  - increase second.x
 
 start{second=GetComponent<secondScript>();}
 
 in update:{
 if eventOccurs:
  - call increaseX}
 
 
 
 //**secondScript**
 start{main=GetComponent<mainScript>();}
 y=main.x
 
 update{
 lots of using y}

vs

 //**mainScript**
 x=3;
 
 update{
 if eventOccurs:
  - increase x}
     
     
 **secondScript**
 start{main=GetComponent<mainScript>();}
 
 update{lots of using main.x}
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
0

Answer by Aviryx · Aug 20, 2020 at 11:29 PM

In the case you mention it would be arguably more efficient to use a static class as it does not require an instance of the class to be created (and a static class can not be instantiated anyway). They provide a convinient container for sets of common methods that "just" operate on input parameters, or can be subsequently used to store data/params that will be accessed frequently/by multiple classes.


The type information for a static class is loaded when the class that references the class is loaded so I think that means it would be more efficient than an actual reference to a class.


A static constructor is only called once meaning a static class remains in memory for the lifetime of the application. This would be more efficient than attempting to load stuff into memory, pull data, then unload it only to reload it again into memory or L1 cache (if you are going to be doing this multiple times).


 public static class ExampleStaticClass
 {
     public static int number;
 }

 public class SomeClass : MonoBehaviour
 {
     void Start()
     {
         Debug.Log(ExampleStaticClass.number);
     }
 }

vs


 public class ClassA : MonoBehaviour
 {
     public int someNumber;
 }

 public class ClassB : MonoBehaviour
 {
     public ClassA classA;

     void Start()
     {
         Debug.Log(classA.someNumber);
     }

 }
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 XirohpueLynx · Aug 21, 2020 at 12:49 AM 0
Share

Thanks. This makes sense, but the second script would be used for more other game objects, each with their own "x" values, would this mess up if I use static classes because they're global. Thanks.

[Edit]-removed first question, it was a misconception. I've searched up more, and I think I understand better how this works, but my second question still stands, if the second script is used for more than just one script, for example mainScript, main2Script and main3Script, would I have to make a separate class for each script and it will become very messy with so many static classes.

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

168 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Spaceship Demo - elements 0 Answers

script type variables 1 Answer

Can't drag text from hierarchy with TextMeshPro 0 Answers

Changing a text equal to a string variable 1 Answer

How do do i reference standard asset scripts in another script? 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