Question by
xSyk · Jan 07, 2016 at 09:57 PM ·
unity5restartnull exception
Script not working after restart unity, giving null exception
So i made one script to fill a roulette based in enums. The enum items will be the roulette items. The script it's work fine but when i restart unity it just give me null execeptions and i need to reset the script. When i do it's working properly again. But when i restart Unity it's giving me the same error.
this is the script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
[RequireComponent(typeof(EnumList))]
public class RouletteUI : MonoBehaviour
{
internal enum RouletteDirection { UP = -1, DOWN = 1 };
public List<Transform> _rouletteItemTransform;
public GameObject _roulleteItemPrefab;
System.Type m_T;
public List<KeyValuePair<string, int>> m_Values;
private int m_Index = 0;
/// <summary>
/// Describes the scroll amount in a given direction
/// </summary>
internal struct ScrollInfo
{
public float amount;
public RouletteDirection _direction;
public Vector3 _referencePosition;
private Vector3 _lastPos;
/// <summary>
/// This method should be called when a drag action starts.
/// </summary>
/// <param name="pStartPosition"> is the mouse position at the beggining of the scroll action</param>
public void BeginScrolling(Vector3 pStartPosition)
{
UpdateScrollAmount(pStartPosition);
this._referencePosition = pStartPosition;
this.amount = 0;
this._direction = RouletteDirection.DOWN;
}
public void UpdateScrollAmount(Vector3 pMousePosition)
{
// are we changing direction ?
if (pMousePosition.y > this._referencePosition.y && pMousePosition.y < this._lastPos.y)
{
this._lastPos = this._referencePosition = pMousePosition;
this._direction = RouletteDirection.UP;
//Debug.Log("Direction changed! Now going UP.");
}
if (pMousePosition.y < this._referencePosition.y && pMousePosition.y > this._lastPos.y)
{
this._referencePosition = pMousePosition;
this._direction = RouletteDirection.DOWN;
//Debug.Log("Direction changed! Now going DOWN.");
}
// Update the amount accordingly
this.amount = this._referencePosition.y - pMousePosition.y;
_lastPos = pMousePosition;
}
}
public float _scrollSensitivity = 0.5f;
public int _itemHeight = 30;
public iTween.EaseType _animationType = iTween.EaseType.spring;
private RectTransform _roulettePanel;
private RectTransform _mainPanel;
private RectTransform _selectorPanel;
private ScrollInfo _scroll;
private UnityEngine.EventSystems.EventTrigger _eventTrigger;
// Selected element index
private int selectedIndex = 4;
private const string ROULETTE_PANEL_NAME = "RoulettePanel";
public virtual void Start()
{
this._scroll = new ScrollInfo();
// Find the roulette child panel
_roulettePanel = transform.FindChild(ROULETTE_PANEL_NAME).GetComponent<RectTransform>();
if (_roulettePanel == null)
Debug.LogError(string.Format("Object '{0}' has no child named '{1}' with a RectTransform component attached to it", this.gameObject.name, ROULETTE_PANEL_NAME));
//Set all child element size to the desired one
LayoutElement[] items = GetComponentsInChildren<LayoutElement>();
foreach (LayoutElement e in items)
{
e.minHeight = e.preferredHeight = _itemHeight;
}
this._roulettePanel.transform.localPosition = Vector3.zero;
InitializeRouleteItems();
}
public virtual void OnBeginDrag()
{
}
public virtual void OnDrag()
{
this._scroll.UpdateScrollAmount(Input.mousePosition);
ScrollRoulette(this._scroll);
}
public virtual void OnEndDrag()
{
}
private void ScrollRoulette(ScrollInfo pScrollInfo)
{
this._roulettePanel.Translate(Vector3.down * pScrollInfo.amount * _scrollSensitivity * Time.deltaTime);
}
/// <summary>
/// Instancia o prefab e cria os itens da roleta
/// </summary>
/// <param name="pText"></param>
private void InstantiateRouletteItem(string pText)
{
GameObject item = Transform.Instantiate(_roulleteItemPrefab);
item.transform.SetParent(_roulettePanel);
item.GetComponent<Text>().text = pText;
UnityEngine.UI.LayoutElement layout = item.AddComponent<UnityEngine.UI.LayoutElement>();
layout.minHeight = _itemHeight;
layout.preferredHeight = _itemHeight;
_rouletteItemTransform.Add(item.transform);
}
protected void InitializeRouleteItems()
{
for (int i = 0; i < m_Values.Count; i++)
{
InstantiateRouletteItem(m_Values[i].Key);
}
}
/// <summary>
///
/// </summary>
/// <typeparam name = "T" ></ typeparam >
public void InitializeWithEnum<T>() where T : System.IConvertible
{
if (!typeof(T).IsEnum) throw new System.ArgumentException("T must be an enumerated type");
m_Values = new List<KeyValuePair<string, int>>();
System.Array values = System.Enum.GetValues(typeof(T));
foreach (object value in values)
{
string name = System.Enum.GetName(typeof(T), value);
m_Values.Add(new KeyValuePair<string, int>(name, (int)value));
}
print("Selected Value" + GetSelectedString() + " : " + GetSelectedValue());
}
private int GetSelectedValue()
{
return m_Values[m_Index].Value;
}
private string GetSelectedString()
{
return m_Values[m_Index].Key;
}
}
So someone can help me with this?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612055343im_/https://answers.unity.com/themes/thub/images/avi.jpg)