#Prefabs and serialized fields issue
1 messages · Page 1 of 1 (latest)
to these serialized fields in a script component:
This works if I do it from a clone in the scene, but if I try to do this from the prefab itself, Unity simply won't let me connect them. And doing it from a clone in the scene doesn't work because the assignment of the values from the databse is thrown off. Does anyone know why this might be happening?
you have to connect the prefab's own text objects to its own copy of the script
sounds like you're trying to drag maybe some objects from the scene into the prefab or something?
Sorry -- pretty new to unity/C#. Thanks for the reply. I'm trying to drag four TextMesh game objects into the four fields on the Hand Builder script. I'm trying to drag those for game objects from a prefab, to the script, which is on a non-prefab game object.
No it doesn't go that way
Ahh, okay.
DO you have a script on the CardGeneric prefab object?
You should put a script on that, and put those 4 fields on that script
and give that script a method like:
public void PopulateCardText(CardInfo cardInfo) {
myCardTitleText.text = cardInfo.title;
myAttribute1Text.text = cardInfo.attribute1;
// etc...
}```
Got it. So it's okay to put the script on the prefab of the thing I plan to be cloning?
then HandBuilder can do this:
Card cardInstance = Instantiate(cardPrefab);
card.PopulateCardText(someCardInfo);
You should 100% have a script on the prefab
It's kind of insane not to
You want each object to have as little knowledge about other objects as possible
it makes things easier to maintain