Hi All,
I’m currently working on a shield system for my player. I’ve got everything working, the object instantiates and is locked to the players transform and acts as it should. However, I want to reference a UI element (slider bar for health) but unity will not let me attach the slider to the prefabs script. I can currently only attached the slider to something that starts in the scene (i.e. player).
Does anyone know a solution to this?
You can’t, unless the object you’re looking to assign is a child of the prefab.
The reason is that the prefab can be used in any scene. So if you set it to the health bar, what should it do if it’s put into a scene without the bar? What if it’s put into a scene with two bars?
What you need to do is store a reference to the bar in some sort of controller class. When you instantiate the prefab, assign the reference to the bar directly to the newly created object.