- Home /
Missing objects upon List.Add();
Basically I have a list in which i need to get instances of a prefab. I instantiate them by using [ExecuteInEditMode]
Here's the line i'm using to instantiate it:
enemyArray [i] = (GameObject)Instantiate (enemy, spawnPosition, Quaternion.Euler (0, 0, 0));
The problem is, when i try to add this gameobject to a list using Add(), i simply get lots of these "Missing GameObject" in the inspector, and i have created the list as new List() alongside declaring it aswell. I try to add them using foreach loop:
foreach(Transform enemy in transform)
{
Debug.Log (enemy.name);
enemies.Add (enemy.gameObject);
}
Weird thing is, it actually debugs the names of the objects correctly. And another weird thing is if i just drag and drop this prefab into the scene, the Add() function will work, it simply won't work on the instances.
If anyone can tell me why it's happening this way, I will really appreciate it. Thanks.
Why use ExecuteInEdit$$anonymous$$ode? Have you tried not using it?
The thing is i'm doing this Add() with foreach loop in Start(). I am not sure but i think if i try to not use ExecuteInEdit$$anonymous$$ode and instantiate it on Start, foreach loop might have nothing to add since it happens on 2 different scripts and it could be over before anything is instantiated yet.
And another reason why i'm using it is because it's kinda easier for me to see where objects will be in game while i'm still in edit mode
Answer by Raresh · Jun 06, 2016 at 09:28 PM
For your usage scenario, i would use Gizmos. https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnDrawGizmos.html http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnDrawGizmosSelected.html
You can assign custom meshes to them, and they only draw in the Editor. This is what you are looking for :D
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to find all similar elements in a list? 1 Answer
Adding item to list error? 2 Answers
List getting reversed? 0 Answers