- Home /
Getting data from a 2d array in Inspector
Hi,
I have a matrix in my GameObject Inspector and I need to access it from another script.
Script to build the Inspector ManholHealth : MonoBehaviour
[Header("Health")]
public Image healthBar;
public Text probabPerc;
public AgingObjects[] colors ;...
[System.Serializable]
public class AgingObjects
{
public Color healtColor = new Color();
public float[] Prob ;
}
}
Result:
What I need is to get the values in the elements of "prob".
Sorry, this is maybe a newbie question, but I am one.
Questions about accessing script properties from other objects / scripts have been covered pretty exhaustively. If you have a more specific question, then feel free to ask that ins$$anonymous$$d.
Answer by xxmariofer · Mar 13, 2019 at 04:38 PM
float value = manHolHealthInstance.colors[0].prob[indexOfTheElement];//just change 0 and indexofelement for the index of the objects you want to access
Answer by Irenemeyer · Mar 14, 2019 at 04:04 PM
@xxmariofer Thank you, but now the next problem, I'm trying to build a matrix like the one below, and I think I managed somehow, but if I try to print it to the console to check it, it doesn't work. Whatever combination I try it comes with the same error. Is there another way to print 2d lists?
//transition matrix
// P[1]= {0.998,0.001,0.0005,0.0005,0.00};
// P[2]= {0.00,0.995,0.003,0.001,0.001};
// P[3]= {0.00,0.00,0.995,0.0025,0.0025};
// P[4]= {0.00,0.00,0.00,0.99,0.01};
// P[5]= {0.00,0.00,0.00,0.00,1.00};
for(int j = 0; j<= manholeScript.colors.Length-1; j++)
{
for (int i = 0; i <= manholeScript.colors[j].Prob.Length - 1; i++)
{
float value = manholeScript.colors[j].Prob[i];
p.Add(value);
//foreach (float data in p)
//{
// Debug.Log(data);
//}
}
P.Add(p);
}
foreach (float[,] data in P) Error: Cannot convert type 'System.Collections.Generic.List<float>' to 'float[*,*]'
{
Debug.Log(data);
}
Follow this Question
Related Questions
Arrays/Lists in the inspector of Editor Extension scripts 0 Answers
Add a new script line to my script with text from textboxes after button in the Inspector is pressed 0 Answers
There is no GameObject attached to this GameObject 2 Answers
Script reference not showing in inspector 0 Answers
Loop problem 2 Answers