another "is it better to this or that" question..

Im working on an interactive storybook… every page is its own scene…
Performance-wise, is it ok to have 10 scripts that do exactly the same thing only slightly differently, or is it better to invest more time and do it “properly”, one clever “modular” script that handles those slight differences internaly?

For example… i have 10 objects that the users can press and something happens… like:

if (Input.GetMouseButtonDown (0)){
if (Physics.Raycast (ray, hit, rayDistance, mask.value)) {
if (Script.RayA01Pressed == false)
{
Script.RayA01Pressed = true;
Script.RayA01Func();
}
}
}

fastes way for me to do it is to create 10 separate scripts and just change RayA01 to RayA02 etc…
I know there is really simple way to handle this example, but i have a lot of scripts like this and it would take me long time to figure out how to do it “properly”…
So, performance-wise, is ti ok to have 50 scripts per scene where those 50 could be replaced by 4 for same result??

Performance-wise I think you would be hard pushed to measure any difference at all. However, from a design point-of-view, the modular approach is vastly superior. The “copy and change” approach may seem faster to you now, but all that time you save will be lost as soon as it turns out there is a bug in your code (which you’ll then have to fix in 10 different spots) or when you want to add new functionality (which you will then have to add in 10 spots). All this with the danger you forget to update one of your 10 scripts and you’re left with a bug that will be extremely difficult to track.

For me, if your scripts are like the example you wrote, it would be less work to make the functionality of the ‘modular’ script than the 10 copies. I realize that this is not the case for you now, but once you get some more experience in doing that sort of thing, it does become the faster way of making such scripts. And the only way of getting that experience, is by trying it ;). It should not be difficult to get some help on this forum should you have questions on how to approach the ‘modular’ solution.

Create a mouse input manager class. Have one raycast , then get the object hit from that and call a “Pressed()” function on that object.

The way you are doing it now every mouse click generates a raycast for every object in the scene, bad form.

Why would you do that? The script should be the same on all objects; creating multiple versions of the same script is definitely a bad idea. (Not related to performance, it’s just really bad scripting practice and makes it far more likely that you’ll introduce bugs.)

Also, instead of raycasting manually like that, you can just use the OnMouseDown function.

–Eric

thanks for the responses. like i said, im not a programmer, i started doing this book app and learning along… i know im doing things stupidly, i just dont know how to do it the smart way… yet…

also, for touches im using FingerGestures now, im not doing raycasting for every mouseclick for every object… (i think :confused: )…