Why does initial value of MonoBehaviour class field disappear after instanciate with GetComponent?

My code is something like this:

class Dot : MonoBehaviour
{
public int size= 100;
//...
}
class GameManager :MonoBehaviour
{
GameObject DotPrefab; //in the prefab there is the Dot.cs code as a component
GameObject DotInstance = Instanciate(DotPrefab);
Dot x=DotInstance.GetComponent<Dot>();
//now x.size=0
//...
}

I want the x.size to be 100 without having to type it down everytime I create another Dot instance.
how does that happen and how to fix it?

If your prefab overrides the variable in the inspector then you should expect this behaviour. Check the value on the prefab.

2 Likes

Yes, I just found it out… I literally spend 2h to find the bug… :frowning:
By the way, thank you so much for answering all the questions I put today! It really helps A LOT.

No problem:)

1 Like