- Home /
Question by
$$anonymous$$ · Feb 28, 2020 at 12:35 PM ·
c#uierrorout of range
Prefab throwing out of range exception when dragged into Hierarchy from Project Folder
So I decided to make a health bar so I was following Brackeys recent guide (How to make a health bar), upon thinking more about it I wanted to make it more than just a health bar. I made a script which is supposed just just Create a prefab in a Canvas, Assign its details, then move it to another canvas (Player,Enemy,etc...).
When I recently loaded up the project and wanted to test another use case dragging the HealthBar Prefab from the Project Folder into the Hierarchy caused a error in the console. I then booted up a different project set it all up again and got the same result. Thank you for taking the time to read this and any help is appreciated!
public class SlidersScript : MonoBehaviour
{
public List<SliderVariables> sliderList = new List<SliderVariables>();
private void Start()
{
//SetUp
SetUpSliders(sliderList);
}
//Testing block
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
//AddValue("Health Bar", 10);
//GradientSwitch("Health Bar", 1);
}
}
/*
* Sets up sliders with values they had put in from start
*/
public void SetUpSliders(List<SliderVariables> sliderList)
{
foreach (SliderVariables slider in sliderList)
{
int childTracker = 0;
Transform childSlidersTransform;
//Create
GameObject newSlider = Instantiate(slider.SliderPrefab, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
//Attach
if (slider.CanvasToAttachTo != null)
{
newSlider.transform.SetParent(slider.CanvasToAttachTo.transform, false);
}
newSlider.name = slider.Name;
slider.SliderReference = newSlider.GetComponent<Slider>();
slider.Fill = newSlider.transform.Find("Fill").GetComponent<Image>();
slider.SliderReference.minValue = slider.MinValue;
slider.SliderReference.maxValue = slider.MaxValue;
slider.SliderReference.value = slider.StartValue;
//Normalized Value turn number to a 0-1 value
slider.Fill.color = slider.Gradient.Evaluate(slider.SliderReference.normalizedValue);
//TODO: Works for now but the ideal is.... Creating the child slider and attaching it somehow????????????
foreach(ChildSliderVariables childSlider in slider.childSliders)
{
//Gets childs reference
childSlidersTransform = newSlider.transform.Find("SliderChildren").transform;
Debug.Log(childSlidersTransform.name);
Debug.Log(childSlidersTransform.GetChild(childTracker).name);
childSlider.SliderReference = childSlidersTransform.GetChild(childTracker).GetComponent<Slider>();
childSlider.Fill = childSlidersTransform.GetChild(childTracker).Find("Fill").GetComponent<Image>();
//Sets childs information
childSlider.SliderReference.name = childSlider.Name;
childSlider.SliderReference.minValue = childSlider.MinValue;
childSlider.SliderReference.maxValue = childSlider.MaxValue;
childSlider.SliderReference.value = childSlider.StartValue;
childSlider.Fill.color = childSlider.Gradient.Evaluate(childSlider.SliderReference.normalizedValue);
}
}
}
/*
* Allows another slider to be added to the list
*/
public void AddSliderToList(Slider sliderReference, int minValue, int maxValue, int startValue)
{
SliderVariables newSliderItem = new SliderVariables();
newSliderItem.SliderReference = sliderReference;
newSliderItem.Fill = sliderReference.transform.Find("Fill").GetComponent<Image>();
newSliderItem.MinValue = minValue;
newSliderItem.MaxValue = maxValue;
newSliderItem.StartValue = startValue;
newSliderItem.Fill.color = newSliderItem.Gradient.Evaluate(newSliderItem.SliderReference.normalizedValue);
if (newSliderItem.CanvasToAttachTo != null)
{
newSliderItem.SliderReference.transform.SetParent(newSliderItem.CanvasToAttachTo.transform, false);
}
sliderList.Add(newSliderItem);
}
void SetValue(string sliderName, float amount)
{
SliderVariables slider = new SliderVariables();
slider = sliderList.Find(x => x.Name.Equals(sliderName));
slider.SliderReference.value = amount;
slider.Fill.color = slider.Gradient.Evaluate(slider.SliderReference.normalizedValue);
}
void AddValue(string sliderName, float amount)
{
SliderVariables slider = new SliderVariables();
slider = sliderList.Find(x => x.Name.Equals(sliderName));
slider.SliderReference.value += amount;
slider.Fill.color = slider.Gradient.Evaluate(slider.SliderReference.normalizedValue);
}
void SubractValue(string sliderName, float amount)
{
SliderVariables slider = new SliderVariables();
slider = sliderList.Find(x => x.Name.Equals(sliderName));
slider.SliderReference.value -= amount;
slider.Fill.color = slider.Gradient.Evaluate(slider.SliderReference.normalizedValue);
}
void GradientSwitch(string sliderName, int gradientNum)
{
SliderVariables slider = new SliderVariables();
slider = sliderList.Find(x => x.Name.Equals(sliderName));
slider.Gradient = slider.OtherGradientPresets[gradientNum];
slider.Fill.color = slider.Gradient.Evaluate(slider.SliderReference.normalizedValue);
}
}
/// <summary>
/// Used to define what a slider is.
/// </summary>
[System.Serializable]
public class SliderVariables
{
public string Name;
public GameObject SliderPrefab;
[HideInInspector]
public Slider SliderReference;
[HideInInspector]
public Image Fill;
public float MinValue = 0;
public float MaxValue = 100;
public float StartValue = 50;
public Gradient Gradient;
public Canvas CanvasToAttachTo;
public List<Gradient> OtherGradientPresets = new List<Gradient>();
public List<ChildSliderVariables> childSliders = new List<ChildSliderVariables>();
}
/// <summary>
/// Used in the SliderVariables class in case a Slider has multiple sliders making it up
/// </summary>
[System.Serializable]
public class ChildSliderVariables
{
public string Name;
[HideInInspector]
public Slider SliderReference;
[HideInInspector]
public Image Fill;
public float MinValue = 0;
public float MaxValue = 100;
public float StartValue = 50;
public Gradient Gradient;
public List<Gradient> OtherGradientPresets = new List<Gradient>();
}
Comment