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

Resolved Arrays and performance

Discussion in 'Editor & General Support' started by dummmmmy, Jul 5, 2023.

  1. dummmmmy

    dummmmmy

    Joined:
    Jun 17, 2023
    Posts:
    12
    hello, ive recently learned how to make custom array class. ive made simple array with enemy info
    upload_2023-7-5_22-50-59.png
    this is how it looks and so far its working great.
    my question is. when im instantiating an enemy. can i use variables Directly from "database"? or do i save it locally in a enemy script and then use those local variables like in this case.
    upload_2023-7-5_22-51-56.png
    while everything i just showed works as i intended, i just wonder if there is any better wayh to do it? does it have any effects on performace? does it have any downsides or upsides?
     

    Attached Files:

  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
    There is absolutely no reason to duplicate all of the fields into local variables. At worst just make a local variable to reference the object from the array:

    Code (CSharp):
    1. SimpleenemyDB enemyInfo = DB.SimpleEnemies[i];
    2.  
    3. // Now you can freely access the data with this variable
    4. Debug.Log($"The enemy's name is {enemyInfo.name} and you can describe it as '{enemyInfo.description}' and it is level {enemyInfo.level}");
     
    dummmmmy likes this.
  3. dummmmmy

    dummmmmy

    Joined:
    Jun 17, 2023
    Posts:
    12
    thanks this seems way easier.