Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Creating class for means of access...

Discussion in 'Scripting' started by Genkidevelopment, Jan 17, 2015.

  1. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    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
     
  2. TheForsaken95

    TheForsaken95

    Joined:
    Aug 29, 2014
    Posts:
    30
    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).
     
  3. sdviosx

    sdviosx

    Joined:
    Jan 4, 2015
    Posts:
    22
    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.
     
  4. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    If multiple of your classes / data types need it why not put it in its own class and inherit from it?
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Equation? Sounds like a perfect job for a static method.
     
  6. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    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... :D

    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!?
     
  7. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    I think this is what I just described above? Thanks :D
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
    Genkidevelopment likes this.
  9. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    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.
     
    Kiwasi likes this.
  10. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Thank you for your input - I will try and avoid what you are describing!
     
  11. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    Think you would have to explain your problem better, but chances are there is a easier way of doing it.
     
  12. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    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 :D

    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...
     
  13. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    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.
     
    Genkidevelopment likes this.
  14. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    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...
     
  15. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    What I am really struggling with, is making a GUI canvass button transform the position of my camera!

    But thanks for your help here...
     
  16. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    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.
     
  17. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    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
     
  18. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    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 }