- Home /
Acessing array / list from Gizmos in editor
Hi, I'm trying to access an element of an array / list that is set in the inspector during "editor time" (not playtime).
Bar contains several values in the inspector.
Doesn't work:
public Bar[] bar = new Bar[2];
OnDrawGizmos()
{
Method();
}
Method()
{
print(bar[1].beerBottles);
}
[System.Serialize]
struct Bar
{
float beerBottles;
}
Works:
public float beerBottles;
OnDrawGizmos()
{
Method();
}
Method()
{
print(bar[1].beerBottles);
}
How can I make the first option work in the editor? Unity claims that the variable in the first option is null even though the variable is set in the editor.
Answer by Berenger · Jun 22, 2012 at 05:04 PM
This code works for me :
using UnityEngine;
public class SerializeAndStuff : MonoBehaviour
{
public Bar[] bar = new Bar[2];
private void OnDrawGizmos()
{
Method();
}
private void Method()
{
print(bar[1].beerBottles);
}
[System.SerializableAttribute]
public struct Bar
{
public float beerBottles;
}
}
If you want Bar to show up in the inspector, you need to declare it as a class, not a struct.
Works just fine today, Unity was playing tricks on me :]. Thanks
Ahh, I figured out the problem. The problem was that the object was a singleton that recreated itself in case it was unavailable. Thus if I disabled the GO where the script was on, Unity created new instance of the script where this value was null. Problem solved. Thanks again
Your answer
Follow this Question
Related Questions
Can a GameObject inside of an array be made "accessible" from the editor? 2 Answers
Assign gameobject icon dynamically 0 Answers
How do you emulate array/List Inspector behavior with an Editor window? 0 Answers
How can I recreate the Array Inspector element for a custom Inspector GUI? 7 Answers
Is it possible to drag/drop/increment into editor array? 1 Answer