Hello everyone!
Is it possible to cache script components?
Aside from performance reasons, I think it’s more convenient being able to access other scripts from various functions right away.
Something like this works:
var OtherScriptOBJ : GameObject;
function OnTriggerStay (Other:Collider) {
var OtherScript_ : OtherScript = OtherScriptOBJ.GetComponent.<OtherScript>();
}
function OnTrigger Enter (Other:Collider) {
var OtherScript_ : OtherScript = OtherScriptOBJ.GetComponent.<OtherScript>();
}
function OnTriggerExit (Other:Collider) {
var OtherScript_ : OtherScript = OtherScriptOBJ.GetComponent.<OtherScript>();
}
While trying to cache like this will give me NullReferenceException Errors in the lines where I try to access OtherScript_:
var OtherScriptOBJ : GameObject;
var OtherScript_ : OtherScript;
function Start () {
OtherScript_ = OtherScriptOBJ.GetComponent.<OtherScript>();
}
function OnTriggerStay (Other:Collider) {
}
function OnTrigger Enter (Other:Collider) {
}
function OnTriggerExit (Other:Collider) {
}
Many thanks for any ideas,
Greetings,
Shu
Your OnTrigger### functions are malformed, you need to pass the correct parameters for them to be picked up by the unity engine.
The whole point of the OnTrigger and OnCollision functions is that they allow you to interact with the thing you have collided with. You can’t cache that at the start since you don’t know what you are going to collide with at that point.
If you’re talking about cache to a completely separate script (some sort of manager script for example), sure cache away so long as that reference is never going to change (or you handle the change).
1 Like
@LeftyRighty
I edited my original post. Actually, I don’t want to access a script on the Collider. It’s a script that is attached to another GameObject that I assigned in the Inspector to “OtherScriptOBJ”.
Assuming you’re assigning via the Inspector - just make an OtherScript instance instead of a GameObject instance and drag-drop the containing GameObject onto the spot in the Inspector.
1 Like
This is awesome! Thanks for the suggestion! It works!
I’ve got two questions about this, though:
- Is there a downside concerning performance? Because I haven’t read about this possibility and it seems much easier to work with.
- Does it work in C#, too?
You’re saving yourself a GetComponent call so I would assume it would be faster to the degree to which it takes to call that method.
Of course it works in C#. You’re doing exactly the same thing, just saving an OtherScript instead of a GameObject.
I do wish they would publicize more the fact that dragging a GameObject onto a script slot in the Inspector will automatically find that script on that GameObject. Clicking the little circle will give you a list of GameObjects with that script as well if you’d rather go that route.
1 Like
@KelsoMRK
Thank you for the answers!
That’s absolutely brilliant. This is so much easier than trying to get the component all the time.
It really should be included when the whole “access script from script” topic is covered.
For me, this reduces the confusion when many scripts are talking to one another quite a bit.
Personally, before knowing about this, I tried doing something similar (drag & drop the actual script file in the Inspector) a bunch of times, but it always failed.