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. Dismiss Notice

Which solution uses less resources?

Discussion in 'Editor & General Support' started by Redden44, Jan 4, 2015.

  1. Redden44

    Redden44

    Joined:
    Nov 15, 2014
    Posts:
    159
    Hi, I'm creating a game where the world has nodes of resources such as plants, mushrooms, minerals, which grow overtime and go through several growing stages.

    I thought about two solutions and I'd like to know which one is less resources consuming pls.

    1- I use a single timer, every x seconds the script checks all the nodes; if a node is active (a node is active as long as the player is nearby), it starts a function which updates the stage of the node. My main concern about this solution is that the script will have to check several hundreds GameObjects every time the timer hits the magic number (maybe once every 5 mins or so).
    The nodes would be children of a GameObject and I would use a foreach loop. Maybe I could easy the loop by changing the parent of the node when it becomes active, therefore I could loop through a shorter list of GameObjects.

    2- Everynode has a timer and it works only when the node is active (as long as the player is nearby). This solution avoids to check all the nodes, though if the player is in a really populated area (a forest for example), there will be dozens of scripts and timers active (lets say max 50-70 nodes).
    I think this solution is better because everynode will have its own timer, although I don't know how heavy could be to have up to 50-70 active scripts/timers at the same time.

    What do you guys think? Thanks :)
     
  2. startas

    startas

    Joined:
    Nov 14, 2014
    Posts:
    102
    Well, unless growing script is really heavy or your game will be for slow phones, 50-70 nodes is close to nothing.
     
  3. Redden44

    Redden44

    Joined:
    Nov 15, 2014
    Posts:
    159
    Thank you. I thought it could be a problem to have so many timers because they're in the Update function and I've read it's better to avoid putting things there. I guess the 2nd solution will do then, thanks again :)

    Mind if I ask another question, it's related to this system and performances.
    I use a child with a collider set as trigger on the player to activate nodes and the floor of the world is made of tiles, thus the trigger keeps triggering the tiles. Is this continuous check of the tiles bad for performances?

    I kind of avoid that with a simple check on the tag of the object the trigger collide with; I was wondering if this is the best solution or maybe there's a way to avoid all these check trough the use of layers.

    Thanks :)
     
  4. startas

    startas

    Joined:
    Nov 14, 2014
    Posts:
    102
    Well, it actually depends on how much action you are doing in your game, if you have unity pro, then you can look at profiler, and if not, then at least you can look at stats(button near button "maximize on pay") and check for numbers. There is no big need to write super optimized code if your game runs fine right now.