3
\$\begingroup\$

I attached the following script to an empty game object (EnemiesGO) in the editor.

public class EnemiesScript {

[System.Serializable]
public struct EnemyWithType {
    public EnemyScript enemy;
    public EnemyScript.Type enemyType;
}

public EnemyWithType[] enemyPrefabs;
public EnemyScript[] enemies;

void Start ()
{   
    enemies = new EnemyScript[enemyPrefabs.Length]; 

    for (int i = 0; i < enemyPrefabs.Length; i++)
    {
        enemies[i] = Instantiate<EnemyScript>(enemyPrefabs[i].enemy);
        enemies[i].enemyCategory = enemyPrefabs[i].enemyType;
    }        
}    

}

When I was populating enemyPrefabs (through the editor) I didn't see the prefabs in the popup selection window so I dragged them directly from the Assets window Assets/Resources/Prefabs/EnemyPrefabs. My question is two fold.

  1. If it is possible, how can I get the populated enemyPrefabs array to be displayed in the game window/ scene window before I run the game? In general, I would like to see changes effected from the editor in the game window before I run the game.
  2. How can I access the EnemyPrefabs folder from the popup selection window?
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$
  • 1st question You should add [ExecuteInEditMode] to your class , check this example. Also, read the description, it explains the differences in the callbacks of playmode/editmode;

  • 2nd question, you should use a GameObject as the type of your prefabs, so you can access them in the inspector via the little circle.

[System.Serializable] public struct EnemyWithType { public GameObject enemy; public EnemyScript.Type enemyType; }

[...]

for (int i = 0; i < enemyPrefabs.Length; i++)
    {
        enemies[i] = Instantiate(enemyPrefabs[i].enemy).GetComponent<EnemyScript>();
    }  
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .