Public MonoBehavior string fields empty
This is bizarre enough that it has to be me being an idiot.
I have a MonoBehaviour
with some public fields and public methods:
public class MyComponentA : MonoBehaviour
{
public string Message = "Foobar";
public int Integer = 42;
public string GetMessage() => "This is a message.";
public int GetInteger() => 42;
}
I've added it to a prefab called Circle_A
.
Circle_B
has a MonoBehavior
that will instantiate an instance of Circle_A
, get the component and log out the values returned from the fields and the methods:
public class CircleB : MonoBehaviour
{
[SerializeField] private GameObject _circleAPrefab;
private void Start()
{
var circleAInstance = Instantiate(_circleAPrefab, new Vector2(10, 10), Quaternion.Euler(Vector3.zero),transform);
var myComponentA = circleAInstance.GetComponent<MyComponentA>();
var message = myComponentA.Message;
LogIt(message);
LogIt(myComponentA.GetMessage());
LogIt(message);
Log(myComponentA.Integer);
Log(myComponentA.GetInteger());
}
private static void LogIt(string message)
{
switch (message)
{
case null:
Log("NULL");
return;
case "":
Log("EMPTY");
return;
default:
Log($"'{message}'");
return;
}
}
}
The integer field and both the methods return as expected, the number 42
and the message`This is a message`. The message field however is always an empty string.
What's happening?
Are you sure the $$anonymous$$yComponentA
attached to the _circleAPrefab
has effectively a value in the inspector?
Your answer
Follow this Question
Related Questions
How to make RequireComponent list possible options. 1 Answer
GetComponent returns null however comparison to null returns false 2 Answers
How to automate removing non-MonoBehaviour derived scripts from a prefab 0 Answers
My enemy don't take damage. 1 Answer
Trigger enabling component works in one statement but not another? 1 Answer