Help me avoid using Find to send information between objects

Hey all,

First off, I’m an artist and not a code guy so really simple is good :slight_smile:

I had a look at the profiler and noticed some of my scripts are resource hogs. I noticed that I was using Find to get data from other GameObjects from the Update function which is bad. I changed that and put the Find in the Start function. This appears to be working but is there a better way?

Thanks,

Andrew

Put up specific code and then we can help you optimize. If you can put it in Start(), Find may not be a good choice anyway. Start might not be the best place, either.

I saw a video on youtube from prime31 about delegates and events but I had trouble understanding how to set it up - I’ll have another look at it.

edit I think I’m starting to understand the event model.

I’m curious about this now, and could probably use some advice. While initially learning Unity and scripting for it, I was extremely sloppy, just figuring out how to make something work. A lot of GameObject.FindWithTag(“GameObject1”).GetComponent(“Script1”) and such, since scripts were attached to all different gameobjects.

I just recently went back through my code and cleaned things up some. Merged most scripts onto a single game manager object and accessed the scripts directly (so from the example above I could just use Script1.somevariablename). So very few things are still using the example above.

Instead of moving everything to one game object, should I have just used the events and delegates model? Or should I continue with what I’m using, and just use events and delegates for the things which are going to require having scripts on separate gameobjects (but still need to talk to eachother)?

Sorry if this is confusing to understand, I’m really just trying to figure out the most common optimized coding structure for mobiles in Unity, rather than just the “make it work” method I was using previously.

If you really must use Find, then be sure to cache it so you only do the Find once. As in…

GameObject heresWhatIFound = gameObj.Find("something");

Now I can access heresWhatIFound any time I want without searching for it again.