The game I’m building loads about 15 enemies when the mission scene is loaded. Currently I have an EnemyScript which is coded to work with the specific enemy the script is attached to. My question is since each enemy is running an instance of this script would it be better to have a single script that is under an empty gameobject that locates each enemy and makes them do everything they need to do so that there arent 15 instances of the script being ran or will it not make a difference? Reason being is when I play the game on my oculus go I’m getting 20 fps apparently but it still seems to play smooth but oculus requires 60 fps so I need to do some optimization before I get too deep. Thanks for any information that is provided!
With the information you provided it is impossible to tell you anything useful.
If you can attach the profiler to your OculusGo then do so and figure out where you’re spending frame time.
If you can’t attach the profiler, put everything under source control (it already is under source control, right???) and start deleting things until you find out what is slowing you down.
I messed with the profiler a few minutes ago but that wasnt exactly my question although I think garbage collection is my issue (see the photo). I already know it’s a combination of enemy npcs and the buildings in my game that are slowing it down. My question was if having a parent script that controls all my NPCs actions would be more favorable than a script attached to each NPC and if it would even make a difference. Hope that makes since.
Or comment out.
Other than that, OP should improve upon punctuation. Is hard to read long sentences on one breath.
Expand (see little arrow) profiler tree and find out exactly, what causes GC. It may be debug log for example.
Also, please use Print Screen, or other tools, to make screen shots. Then you can paste directly to message here.
Forget this issue I found out it was because the console was showing errors because a referenced object from another scene was not loaded. So back to my original question.
would having a parent script that controls all my NPCs actions be more favorable than a script attached to each NPC and would it even make a difference.
That’s a great question, one the profiler can answer for you.
There’s more to life than performance: if you write crazy performant code and you can’t read it two months from now, you haven’t done much good. Write the problem how you naturally think about solving it and around 99% of the time that will be good enough.
I get that. I was trying to make sure before I rewrite my whole enemy script if it would really make that much of a performance change. Guess I dont have an option other than trying so now I’m just going to set a general variable script on each npc then have a enemy manager script loop through a list of the active npcs and for each one do the actions necessary.
Uh… what happened to the option of running the profiler?
I think the idea of controlling all of them though a manager is to cut down on MonoBehaviours on the scene, if you put a tag script and control it from the manager i think it’s not gonna make a difference.
To answer the OP, generally yes, but in the case of 15 instances I don’t think it’s gonna make a real difference,
I once had a test project with thousands of MBs, each with a little gravity script, it would crawl to a halt once you got about thousands in there.
I tested two solutions:
-
use a manager script to call the logic in a custom function, instead of calling it though FixedUpdate (the unity callbacks have a certain amount of overhead to them), was able to more then double the amount of objects live.
-
completely remove the script from the rigidbodies and do everything in the manager, was able to get it up to a little over 10K.
*all the logic was is just the gravity formula, heaviest thing per iteration was a square root.
*this was tested on 5.6 and a few versions of 2017, all with the same result, if you wanna do something like this now look up the new DOTS system.
*I also have a 7700K, it also has a factor with the numbers here.
if you have more logic on a script, plus personal variables etc. I would keep them all separated but instead of using Update/FixedUpdate/we call it “MyUpdate”(or whatever) and call it from the manager.
still shouldn’t be much difference with only 15 instances but it’s such a small thing to change I wouldn’t even call it refractoring.
Running the profiler isnt going to tell me if code I havent written yet will be more efficient. It tells me two things my materials on buildings in my game are costing me about 10fps so I need to use different shaders and the enemies in my game are hitting my fps pretty good as well.
I think you’re right that only 15 instances shouldn’t really make a difference but I think the reason the 15 instances are so detrimental to my fps is because of my lack of using coroutines and my dependency of using Update() to make the enemies do their actions. Thanks for the reply!