when i type this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
slider Slider1 = new slider();
}
// Update is called once per frame
void Update()
{
}
}
it says slider.Slider1 is not accessible due to its protection level
Yes, that message is a bit confusing. What’s happening is that you are trying to create a component using the new
keyword.
Don’t do that, instead, declare your slider as a public global variable, and attach your slider in the inspector.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TestScript : MonoBehaviour
{
public Slider slider;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
Or, if you want to, you can add your slider from code. Thought that would in many cases be more complicated than it needs to be, since you would have to add all the graphics and stuff in code.
To add components in code, use gameObject.addComponent<Slider>();
(or whatever type of component you want to add)