How to cache this?

Hi folks, just a fast question i have this sentence:

laser l = GameObject.FindWithTag (“Player”).GetComponentInChildren ();

And it is on update function, so i wanna cache that GameObjectFindWithTag stuff, but i dont know how, and i dont see on the API how to do it…

I mean something like that on awake/start function

//var declaration
Transform target
//awake function
target = GameObject.FindWithTag(“Player”).transform;

I’m sorry, but I think you answered your own question or I misunderstand your problem. What is it exactly what you need?

1 Like

If i put this sentence on the update function it will lag me a lot due to frame-to-frame check
laser l = GameObject.FindWithTag (“Player”).GetComponentInChildren ();

So to avoid that lag, i usually do the cache stuff like this on awake/start function:
target = GameObject.FindWithTag(“Player”).transform;

To prevent using the FindWithTag stuff on the update function. The problem is, when i used that (target sentence) i need a Transform var (Transform target;) because im using a transform value. But with the first sentence i dont know for sure what kind of var is… :S

It is an instance of laser. To store it you should use:

var target : laser

You should try to use capital letters when naming a script. It makes it easier for you in the long run.

1 Like

No i think you are not understand me. I mean im using c# and Laser l= var l: laser on javascript. I

I want to cache the another part… I mean its in the update function so… i dont wanna do each frame a GameObject.FindWithTag(“Player”).GetComponents(); So i have to cache that sentence on awake/ start function for not causing lag…

Target sentence was an example… So if i want to check the player position on the update function and i dont wanna have lag at all ill cache that sentence on the start/awake void like this:

target = GameObject.FindWithTag(“Player”).transform

So… if i do that, i can use “target” on the update function without any lag… So thats a transform var, i want to do the same thing with this (no with laser, because var target:laser on the var declaration and laser target=… its the same):

laser l = GameObject.FindWithTag (“Player”).GetComponentInChildren ();

…but you are answering your own question:

“i dont wanna do each frame a GameObject.FindWithTag(“Player”).GetComponents(); So i have to cache that sentence on awake/ start function for not causing lag…”

Why don’t you just do that?

Because i dont know how to cache that sentence ^^. In the case of “target” i can save that sentence in a var with a Transform value due to is a transform type. I cant save the "GameObject.FindWithTag(“Player”) as a transform value.

I mean i know what to do, but i dont know how to do it…

That is because it is a gameObject.

You want:

GameObject.FindWithTag("Player").transform
1 Like

Lol, so its = what a stupid question. Thanks @Fluzing