- Home /
How to access a GUI element in an instantiated prefab?
Hi,
I have created a Prefab which contains a slider, for example sliderResonance. Now, when I instantiate the Prefab from code, I cannot find a way to access each individual sliderResonance and set its value from code.
You can think of this application as collection of softsynths; I want to create several identical "musical instruments", each which can play their own sounds. The user shall be able to load many of these, thats why I instantiate them from code.
Now, another part of the application, the sequencer, needs to be able to access and modify any of these sliders individually.
Example code:
public class TB1 : MonoBehaviour {
public Transform Synth1; // This is the Prefab, which works as expected when alone
void Start()
{
Instantiate(Synth1, canvas[canvasApp].transform, false);
Synth1.transform.localScale = new Vector3(1, 1, 1);
Synth1.transform.localPosition = new Vector3(0, -334, 0);
Instantiate(Synth1, canvas[canvasApp].transform, false);
Synth1.transform.localScale = new Vector3(1, 1, 1);
Synth1.transform.localPosition = new Vector3(0, -1000, 0);
}
}
Now the above seems to work fine; the Prefabs are loaded into the game, and they work as expected. BUT when I try to access the sliders, see code below, only one (random) gets accessed, I guess it is because they have the exact same name, since they are instances of the same Prefab:
Slider test1 = Synth1.Find("sliderResonance");
test1.value = 50;
How on Earth do I access a specific one??? Sorry for being quite new at this; all help is appreciated.
Your answer
![](https://koobas.hobune.stream/wayback/20220612110928im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Strange GUITexture prefab problem with pixelInset.width 1 Answer
Deleting a Prefab when it is dragged to a GUI Texture 0 Answers
Canvas with GUI elements in prefab act strangely 0 Answers
Why is it important to create an empty gameobject for my prefabs? 0 Answers
Instantiating in Awake() v Setting up prefab instances in Editor performance difference? 1 Answer