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

What's the best algorithm for communicating b/w diff. Scripts and objects??

Discussion in 'Scripting' started by sarthakz9, Jun 5, 2015.

  1. sarthakz9

    sarthakz9

    Joined:
    Jun 3, 2015
    Posts:
    19
    I'm just a newbie...
    & ,I just work on 15 diffrent scripts and objects having too many variables... now I wish to combine them... Use them together

    So what's the best way to communicate & access , the info from these scripts..in one single script.

    Help me guys,
    Thanks..
     
  2. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    15 script components using all the same class, or 15 script components using a individual class each? :)

    Static variables would be one way to clean up.
    Using a single "data" class and referencing it in each of your scripts could be an other.
     
    sarthakz9 likes this.
  3. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    sarthakz9 likes this.
  4. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    Cover the basics first, statics are no good unless your got singal instance objects, and events are useful but not always the soultuon but no one here mentioned simply using GetComponent to get a reference to the instantiated script.
     
    Swearsoft and sarthakz9 like this.
  5. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    We were saving that idea for you to suggest, obviously.
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Static is bad for beginner, and should be avoided. Don't use it just to enable script to script communication.

    You can use GetComponent.

    And here is some shameless self promotion.

     
    Deleted User and Ryiah like this.
  7. sarthakz9

    sarthakz9

    Joined:
    Jun 3, 2015
    Posts:
    19
    thank you.... i will love to start with basic....
    the video is good... explains a lot ... but can be more interesting ... if u include some example codes in it ...;)
     
  8. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    If your scared to use Static, better not give out advice to others, not to use it. Static singletons are great, and can be the keyrole to a manager like a poolmanger ( to avoid overheads / GC) scripts can reference each other through this poolmanager . Avoid using GameObject.Find to get GetComponent reference, this is very bad practice , this will guaranty slow your game down no matter how you use it. If your going to use find, use it to loop transform child go, never a gameobject from the scene.

    A poolmanger is basically a class that handles objects, so they can be reused and accessed without causing overheads and laggspikes.

    Dont listen to people who say dont use static, they lack the knowledge how to use them correctly.

    But read this document before using any static so you don't use it wrong, not hard or completed for a beginner.
    http://wiki.unity3d.com/index.php/Singleton
     
    Last edited: Jun 6, 2015
    sarthakz9 likes this.
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I'm not scared of static, I know perfectly well how and where to use it. I also know it's not the right tool for sharing variables between scripts. And it's definitely not for use before you learn GetComponent.

    Singletons and object pools also have their places. Again, not the first tool you should learn or reach for.

    There are also a ton of design patterns that can be used for organising code. Again, useful in the right places. But not everywhere.

    It's also possible I missed the tone of the OP, but he doesn't strike me as a coder ready to jump into the Gang of Four just yet.
     
  11. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    Static class or varbiales has nothing to do with GetComponent or script communication in general. You can't use GetComponent if you dont have the reference, GetComponent is just a simple syntax to access the components on the object. So you cant really say use GetComponent instead of statics, that is miss leading. In most case you dont even need the GetComponent. You can just use this.transform.renderer.material

    Design patterns are used to make your project more structured, ofc you use the pattern at the right place. There are many good threads on this forum about pool-managing. Actually poolmanaging was the first thing i started to learn 5 years ago when i started with game development, Reusing objects is a must for any one who wants to make a game. Teaching them to use GameOject Find is just wrong.

    I understand you want to explain things simple for OP that is good, but sometime its better that they learn things the right way, When starting with script communication.

    Using GetComponent when objects collide is a good practice i agree. But never find..you use a manager for that.
     
    Last edited: Jun 6, 2015
  12. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    As you wish @Ironmax. You're posts show a bunch of misunderstandings about the framework and engine that I can't be bothered getting into now. Best of luck with your future projects.
     
    passerbycmc likes this.
  13. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    No one mentioned the use of GameObject. Find we just said it is good to understand GetComponent since it is the main way in unity to get a component reference.

    No one said statics are bad either just said they are often not the tool for the job unless used with some kinda manager class.

    The op was talking about basic object to object communication not about singalton manager objects.
     
  14. Noxzo

    Noxzo

    Joined:
    Feb 16, 2014
    Posts:
    14
    i wonder how you can use GetComponent without a gameobject
     
  15. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
  16. Noxzo

    Noxzo

    Joined:
    Feb 16, 2014
    Posts:
    14
    Where do you think you find the script Component? Are you going to loop throug 1000 of objects as a code dicipline? fansicanting..

    Btw did you read your own link? Its not advised to use, as it is slow. But maybe you know better than Unity.

    From your link "Please note that this function is very slow. It is not recommended to use this function every frame. In most cases you can use the singleton pattern instead."
     
  17. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    You didn't say anything about optimization in your post. You asked how I gave an answer.

    It says not to use in an update, not that you shouldn't use it.
     
  18. Deleted User

    Deleted User

    Guest

  19. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    They aren't bad when they are the proper tool for the job. People just need to learn there proper use.
     
    Kiwasi and Fajlworks like this.
  20. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    I heard spaghetti code is the new design pattern.. 100% safe from staticness
     
    Last edited: Jun 6, 2015
  21. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    Aside from statics are usless for working with objects that have multiple instances.

    There are a lot of ways to do things and some solutions are better for some situations. Statics are only good for 1 off methods that don't interact with a object, or for getting a reference to a singalton object.

    GetComponent is the main way in unity you get references to other objects. Just grab your referencs and store them in variables in your start method, than use the references later.

    Neither results in spaghetti code, spaghetti code is the result of poor planning.

    For beginners GetComponent will do the job, but as you learn you should take time to learn about events, statics and singaltons and the use cases they become useful in.
     
    sarthakz9 likes this.
  22. sarthakz9

    sarthakz9

    Joined:
    Jun 3, 2015
    Posts:
    19
    well,
    getcomponent and gameobject.find are working fine with me...they have done there job....
    pool managing sounds intresting...so i will learn that next....:D
     
  23. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    What situation are you using GameObject find, since you can also just make public fields for your components and assign via drag and drop as well. Which is generally better than using find.
     
    sarthakz9, Kiwasi and ThermalFusion like this.
  24. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    GameObject.Find should be removed from Unity.
     
  25. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    There is nothing wrong with it, it just depends on how you use it, just like anything else.
     
  26. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    Yes but there are very few situations where it is a good solution. Since getting objects by string name can be pretty bug prone.
     
    Ironmax likes this.
  27. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I seldom use any of the Find methods, but they do have there uses. I occasionally use them as part of scene loading.
     
  28. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    Ya same even between all my own projects and at work I can only think of a handful of times when I used find, and it was for some pretty fringe circumstances.
     
    Ironmax likes this.
  29. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    + 2 for passerbycmc. There is also a big difference from GameObject.Find and Transform.FindChild (that is very useful)