i want to create a scrollbar in unity through / via a c# script.
what are the min requirements (components to add) in order to do so?
so far i have created a GameObject for this, and have added a Scrollbar as component, and have added a RectTransform as the Scrollbar handlerect
the rest of the settings i attempt to change in the scene or game panel once i run the scene / game / script, like size and position and value.
but the scrollbar is not visible in the game panel when i run the scene.
is it do to a wrong setting, or is something missing…?
i tried looking for an example of creating a scrollbar through a c# script but did not find one
This is a very hard thing. Instead, make a prefab of the slider and clone it.
If you insist on doing it, then make one in the editor and see what values Unity puts into its fields, perhaps writing some code to show you what all those fields are.
Instantiating things into a UnityEngine.UI (aka, com.unity.ugui) Canvas hierarchy:
Everything instantiated in a UI MUST be parented to something valid in the hierarchy.
Therefore, if you instantiate prefabs into your UI hierarchy, always use the version of Instantiate that takes two arguments, with the second argument being the parent.
ALWAYS supply a valid under-a-canvas parent Transform where you want the UI chunk to appear.
Supplying positions to anything in UI space via code is almost always an exercise in futility. This is due to the inscrutable nature of the RectTransform object and how it interacts with other RectTransforms, anchors, and the CanvasScaler as well as the canvas mode.
You are of course welcome to supply positions but it is on you to test on EVERY possible combination of aspect ratio and resolution to verify your positioning and anchoring. Expect it to fail if you do not do this testing.
Instead of supplying positions, either use a flavor of the Layout family of components to position things automatically, or else use invisible pre-existing Transforms in your UI as positional inputs.
Here’s why RectTransform stuff is hard: it takes 15+ lines of code just to move the anchors to the corners:
Unity 2023.2’s release especially as it simplified the creation of custom controls by replacing all of the boilerplate with attributes. Here’s a video that shows it off starting around the 56 minute mark.