Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Is working with other scripts expensive?

Discussion in 'Scripting' started by LeLoicLe, Aug 21, 2020.

  1. LeLoicLe

    LeLoicLe

    Joined:
    May 27, 2020
    Posts:
    25
    HI
    Stupid Question: I was wondering if working with other scripts and their variables (As variable in main script and linked in the inspector or by getcomponent<>() ) expensive in performance?
    Thank you. :)
     
    HaiderBassim04 likes this.
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    No.
     
    Dextozz and LeLoicLe like this.
  3. LeLoicLe

    LeLoicLe

    Joined:
    May 27, 2020
    Posts:
    25
    Thank you, this was crazy fast. :p
     
  4. Xepherys

    Xepherys

    Joined:
    Sep 9, 2012
    Posts:
    204
    A variable in memory is a variable in memory, regardless of which class it's associated with. A single class can be put in multiple scripts (using the
    partial
    keyword), and multiple classes can be in a single script. The performance isn't related to either how they are spread across scripts or which class contains a variable being accessed.
     
    HaiderBassim04 and LeLoicLe like this.
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    To elaborate more - operating on variables and methods within the same object, and operating on variables and methods on another object are completely identical performance-wise for the computer. Object oriented program is just an abstraction. This becomes more clear if you start using the
    this
    keyword to break down the illusion a little bit. For example. Take these two classes, A and B:
    Code (CSharp):
    1. public class B {
    2.   public void BMethod() {
    3.     // Do some stuff
    4.   }
    5. }
    6.  
    7. public class A {
    8.   public void AMethod() {
    9.     // Do some stuff
    10.   }
    11. }
    Now pretend we have some code that lives in a method in A:

    Code (CSharp):
    1. // THIS CODE IS INSIDE CLASS A
    2. // Assume this was assigned somewhere
    3. public B referenceToB;
    4.  
    5. void Start() {
    6.   AMethod();
    7.   referenceToB.BMethod();
    8. }
    You might ask: is it slower to call
    referenceToB.BMethod()
    than to just call
    AMethod()
    since the former is on a different object?

    The answer is resoundingly no. They are just as fast. Let me show you some identical code that might shed some light on why. It would be equally valid to write the same code like this:
    Code (CSharp):
    1. void Start() {
    2.   this.AMethod();
    3.   referenceToB.BMethod();
    4. }
    We've now introduced a
    this.
    to the AMethod() call.
    this
    is just a reference to the current object. In reality, every time you call
    AMethod()
    from inside class
    A
    , there is implicitly a
    this
    reference being used.

    If you start to think about it you will see that Object Oriented Programming is just an illusion. It's a little syntactic sugar that essentially just adds an implicit extra function parameter to every function call: a reference to the object on which the function is being called. There is no magic involved in calling a function on
    this
    object vs another object. Under the hood, the program is taking a reference to an object, whether it's some other reference like
    referenceToB
    , or
    this
    , and simply using it as an extra function parameter.

    TL;DR calling functions on other objects vs this object is exactly the same performance-wise because objects are just a high-level abstraction that don't really have any meaning to the computer.
     
    Last edited: Aug 21, 2020
  6. LeLoicLe

    LeLoicLe

    Joined:
    May 27, 2020
    Posts:
    25
    OK!
    Thank you so much!
    So no difference cause they stay as 2 objects, did not know that. :)
    That helps me very much. :)
     
    HaiderBassim04 and Xepherys like this.
  7. pantang

    pantang

    Joined:
    Sep 1, 2016
    Posts:
    219
    No but doing lots of gets and finds can be, if your getting multiple values or calls to the same object its best to find the component once and store it.
     
    Xepherys likes this.
  8. Xepherys

    Xepherys

    Joined:
    Sep 9, 2012
    Posts:
    204
    Yes, this is very true. Get and Find operations are, generally, ill-advised. If there's any other way, that other way is probably better. There are some instances where Get/Find are just the option you need to go with, but as pantang said, do it once and be done. If you need access to something a variety of times where a Get/Find would be required (or different objects might Get/Find the same other objects), make a manager class that sits outside of those objects and can Get/Find once and cache the object for other objects to reference.
     
    pantang likes this.