- Home /
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}
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);
}
}
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
Follow this Question
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