- Home /
Passing Object References to Other Script
I have a scriptable object with several GameObject variables which I fill in the inspector with some prefabs. They are parts of armor I'd like to instantiate to my player instance by calling a method inside the player script. So far I made it work by passing the references with a method call inside the scriptable object. To make it simple, I wrote something similar like this:
public class ScriptableArmorObject : Equipment { Player player;
public GameObject helmet;
public GameObject bodyPart;
public GameObject legPart;
void Start()
{
//Find player and get Player component;
}
void Equip()
{
PassReferences();
player.SetArmor();
}
void PassReferences()
{
player.helmetRef = helmet;
player.bodyRef = bodyPart;
player.legRef = legPart;
}
}
I made it work with no problem, but here come my question, is there a better performance-wise way to do this? I mean I have 8 part references to pass, and inside the Player script I also need to declare 8 variables to hold these references. I've tried to pass the ScriptableArmorObject to my Player script and access the references there, but I got some problems when it comes to swap my equipped armor. Hope someone can helps, thank you!
a scriptableobject with 8 vars is heavier than just 8 vars. passing 8 vars is really fast and you are not even doing it all the time
What where those other problems you mentioned ? To me it sounds like you should look there.
Answer by Esteem · Jul 31, 2019 at 03:22 PM
if you're worried about performance with 16 references on two objects you're probably worrying about the wrong things.
To put things into perspective:
most modern CPUs are running upwards of 3Ghz. CPU such as that is capable of performing 3 000 000 000 cycles per second.
Now forgive me the inaccuracy, but accessing a reference type takes approximately
(depending on many things but for our example it's good enough) 50-150 cycles.
You can think of it as accessing and setting your 16 reference type variables takes approx.:
100 * 16 (*100 for some additional random overhead = very VERY generous) = 16 000 cycles.
3 000 000 000 / 16 000 = 1/187 500 which is 0.00000053 % of your CPU's power at any given frame
now ask yourself:
Do you need to care about performance in such scenario?
his second approach would be even slower, may i ask you something? do you know how much would cost calling a method vs accessing properties?
I am not sure what do you mean by that. From what I can gather, shining_gentleman is slightly new to program$$anonymous$$g and he fears the performance hit his application will take by doing things such as this. I made a simple comparison to put him at ease because these kinds of performance optimization aren't necessary and in time these issues will get ironed out.
I know, just wanted to point out that having a var if the type of the scriptable object would be heavier than just the 8 vars
Thanks for all your replies guys, it does help me a lot in understanding what I'm actually doing. Well, even though I just started to learn Unity and I have no background at all in program$$anonymous$$g, but I guess I get the Idea of what you guys are trying to explain, I'm truly grateful. To answer @Esteem questions, you've guessed everything right! This equipment system I've been trying to implement for my project is the first complex scripting (at least to me) I've ever done. Others are just bought asset, reading documentation, and following a tutorial. I was just wondering if there is a better way (more programmer-way) in doing that, because that's all the ways I can think about. I'm really interested in program$$anonymous$$g, thus I'd like to know things that I might do wrong as early as possible and improve.