- Home /
Can I use the new GUI system with instantiated prefabs?
I am trying to create a health bar that is controlled by a script attached to the player. I made the slider with the new UI system and is in the scene. I also have a variable called
public Slider healthSlider
When I try to drag the slider into the healthSlider field, It does not stay and, therefore, I cant control the slider as it is never referenced. Is there a way to control the slider with a c# script attached to a prefab(player)?
Comment
Answer by Kiwasi · Jan 03, 2015 at 07:53 AM
You can't assign a scene object to a prefab, as prefabs are universal across scenes. What you can do is assign the value after instantiating. Pseudo code.
GameObject clone = (GameObject) Instantiate(MyPrefab);
clone.GetComponent<Slider>().value = 6;