Creating class for means of access...

I have the same equation running in multiple scripts…

Am I better off, creating one script with the equation and getting the relevant game objects to access that class and import the result?

Is there much performance penalty for accessing classes? I imagine lots of smaller scripts interacting such as a node tree, resulting in better performance… Is this assumption correct

Thankss

Peace

I’m not super experienced with minor details such as this, but I’ve heard before that dividing your scripts into several components has no impact on performance. However I’ve also heard that using multiple GameObject.Find() in your code can be very demanding (Especially if called every frame inside of update).

The benefit of splitting functionality to different classes helps aid in code readability, scaling, efficiency, and debugging. You can follow this common design practice however that will not exclude you from having bad performance especially if in one of your classes your are using a heavy load intensive function or are initializing objects left and right and passing them around.

If you have data that needs to be passed around many objects you can use the singleton pattern.

If you are having performance issues, debug your application and check which functions are causing heavy loads on the GPU or CPU.

If multiple of your classes / data types need it why not put it in its own class and inherit from it?

Equation? Sounds like a perfect job for a static method.

I’m a two week old programmer so almost everything you guys have mentioned surpasses me!!! But thanks, I’m sure in future weeks, upon reading back, my understanding will grow to deem all of your answers worthwhile!

I was more wondering if this was a fundamental… Seems like it’s more of a ‘make things easier’ if the **** hits the fan type practice… :smile:

Basically, I have two dozen game objects basing their position on another single object. I was wondering whether to calculate each of their positions in one long script and have them each search for it, or the other way, each searching for the raw data and then calculating their position within their own script…

As it seems the transfer of data from one script to another is not desirable… I think creating one bigger script to do ALL of the calculations will be best… This way only 22 bits of data will be ‘class swapped’ as opposed to 166…

Does my reasoning seem logical!?

I think this is what I just described above? Thanks :smile:

A would strongly avoid making classes bigger. Best practice is small modular classes. Best practice is also not to repeat any code.

If you are writing the same code twice it should be a method call of its own. Doing otherwise is asking for a maintainability nightmare.

1 Like

Something i never want to have to deal with again when something goes wrong.
I did just that thing a few times a couple years ago when i started and it was so impossible for me at that point to figure it out that I ended up starting over lol.

1 Like

Thank you for your input - I will try and avoid what you are describing!

Think you would have to explain your problem better, but chances are there is a easier way of doing it.

It’s complicated! But working, now I am working on calculating most of the data in a master script, with all objects seeking their data from it… This way the amount of data swapping classes is at a minimum and the number of repeated equations is lower :smile:

I’m not having any performance issues yet, I am not far enough into the project to be experiencing that , but as I am learning from scratch, I wanted to make sure I was practicing some better habits from the off…

You know sending data between objects isn’t that bad unless you are using GetComponent too much. A good way to pass lots of data is to just make your own struct that contains all of said data that needs to be transfered, with any methods built in that are useful for manipulating that data.

1 Like

Yes that is what I am doing I believe… Creating an empty gameObject with script attached, script awakes/starts all relevant data, calculates it… all other classes reference it and cherry pick the bit they need…

What I am really struggling with, is making a GUI canvass button transform the position of my camera!

But thanks for your help here…

For making that happen I would put the script that moves the camera right on the camera and use the on click callback on the button to run a method on your camera script.

Thank you… I have just added a debug script on an empty game object named ‘_ButtonManger’… I now have it so debug string names are being listed upon clicking each button in the console…

I am guessing I can use this approach to ‘activate’ or call the script placed upon the camera… All I need to do is create an if… OnclickButtonID is true … perform transform.position… within the camera script… listening for the relevant string ID coming from the _ButtonManager?

Is my thinking correct here?

Thanks Peace

I am using this code to generate the debug

1 using UnityEngine;
2 using System.Collections;
3 public class ButtonsScript : MonoBehaviour
4 {
5 public void ClickButton(string buttonID)
6 {
7 Debug.Log ("Click on " + buttonID);
8 }
9
10 }