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 way of using the UnityEngine namespace is more Efficient/Performant?

Discussion in 'Scripting' started by LDawson97, May 22, 2019.

  1. LDawson97

    LDawson97

    Joined:
    Aug 16, 2018
    Posts:
    19
    Hi,

    I'm making a small mobile application focused on efficiency, though what i've been taught is theoretical, and not related to Unity; mainly algorithms.

    I have an object pool of 100 enemies, and I am instantiating each enemy into an array position using Unity's Instantiate method. Now here is the question.

    Which would be more performant?
    • using the "using UnityEngine;" to access the namespace, then use Instantiate() to instantiate the enemy
    • using "UnityEngine.MonoBehaviour.Instantiate" to instantiate an enemy (without the "using UnityEngine;" statement.
    Here is my script:
    Code (CSharp):
    1. public class cardEffect_SpawnEnemies : cardBase
    2. {
    3.     public override void Effect()
    4.     {
    5.         for (int i = 0; i < EnemyPool.gPool.maxRiflemanEnemies; i++)
    6.         {
    7.             EnemyPool.gPool.Enemies[i] = UnityEngine.MonoBehaviour.Instantiate( EnemyPool.gPool.enemyRifleman).GetComponent<enemyBase>();
    8.             EnemyPool.gPool.Enemies[i].Instantiate((byte)i);
    9.         }
    10.  
    11.         for (int i = 0; i < EnemyPool.gPool.maxTankEnemies; i++)
    12.         {
    13.             EnemyPool.gPool.Enemies[EnemyPool.gPool.maxRiflemanEnemies + i] = UnityEngine.MonoBehaviour.Instantiate(EnemyPool.gPool.enemyTank).GetComponent<enemyBase>();
    14.             EnemyPool.gPool.Enemies[EnemyPool.gPool.maxRiflemanEnemies + i].Instantiate((byte)(EnemyPool.gPool.maxRiflemanEnemies + i));
    15.         }
    16.     }
    17. }
    Thanks!
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Isn't the goal of a pool to minimize instantiating, and moving the bulk of all instantiations to the very start? If so, you'd be micro-Managing a tiny aspect of the Project. This would be time better spent on getting the whole project to run, be a fun experience, and without bugs. If and when you come across Performance issues, use the Profiler to identify likely candidates for optimization. Don't try to pre-solve issues you may not even have - especially if they may depend on the compiler / linker / runtime.
     
  3. LDawson97

    LDawson97

    Joined:
    Aug 16, 2018
    Posts:
    19
    It is. That is why I only instantiate an enemy once, and this is at the very start. I need to justify the use of optimization techniques throughout the whole project, as best as I can, so I can't leave optimization till the end. This is also more a technical demonstration, not a game, so it doesn't necessarily need to be fun.

    I want it for future reference, so I know which is more performant. I'm not pre-solving anything, as I don't know if there is anything to be solved yet. That is the point of this thread. If you don't like the question, don't answer it.
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    It absolutely does not matter how do you access class name in your code.
     
    lordofduct and SparrowGS like this.