Hello I just been trying to add a spell action-bar and i get a null reference error.
Here is my codes.
The HUD:
private var icon1 : Texture2D;
private var icon2 : Texture2D;
private var highlight : Texture2D;
private var activated : Texture2D;
private var slotBG : Texture2D;
private var clickInfo = new Array();
private var hoverInfo = new Array();
private var dragMode : boolean = false;
private var actionList = new Array();
private var bagList = new Array();
function Awake () {
icon1 = Resources.Load("GUI/ActionbarGUI/action-icon-1");
icon2 = Resources.Load("GUI/ActionbarGUI/action-icon-2");
highlight = Resources.Load("GUI/ActionbarGUI/slot-highlight");
activated = Resources.Load("GUI/ActionbarGUI/slot-active-highlight");
slotBG = Resources.Load("GUI/ActionbarGUI/slots-bg");
}
function Start() {
actionList = new Array(12);
for(i=0; i<actionList.length; i++) actionList[i] = new Array(4);
for(i=0; i<actionList.length; i++) {
actionList[i][1] = Rect(203+(i*29),203, 24, 24);
actionList[i][2] = new Array("action");
}
actionList[0][3] = KeyCode.Alpha1;
actionList[1][3] = KeyCode.Alpha2;
actionList[2][3] = KeyCode.Alpha3;
actionList[3][3] = KeyCode.Alpha4;
actionList[4][3] = KeyCode.Alpha5;
actionList[5][3] = KeyCode.Alpha6;
actionList[6][3] = KeyCode.Alpha7;
actionList[7][3] = KeyCode.Alpha8;
actionList[8][3] = KeyCode.Alpha9;
actionList[9][3] = KeyCode.Alpha0;
actionList[10][3] = KeyCode.Minus;
actionList[11][3] = KeyCode.Equals;
bagList = new Array(12);
for(i=0; i<bagList.length; i++)
bagList[i] = new Array(3);
for(i=0; i<bagList.length; i++) {
bagList[i][1] = Rect(203+(i*29), 237, 24, 24);
bagList[i][2] = new Array("action");
}
}
function Update (){
if(Input.GetMouseButtonDown(0))
storeMouseLoc();
if(Input.GetMouseButton(0))
dragCheck();
if(Input.GetMouseButtonUp(0))
compareMouseLoc();
}
function OnGUI(){
hoverInfo = new Array();
GUI.DrawTexture(Rect(200,200,349,30), slotBG);
GUI.DrawTexture(Rect(200,234,349,30), slotBG);
if(GUI.Button(Rect(200,300,200,25), "Add items to action bar")){
var openSlot = getNextSlot(actionList);
if(openSlot != -1){
actionList[openSlot][0] = new classFireBall();
}
}
if(GUI.Button(Rect(200,330,200,25), "Add items to bags")){
openSlot = getNextSlot(bagList);
if(openSlot != -1)
bagList[openSlot][0] = new classSnowOrb();
}
for(i=0; i<actionList.length; i++){
if(actionList[i][0] != null)
drawButton(actionList[i][1], actionList[i][0].icon, i, actionList);
else
checkRectHover(actionList[i][1], null, i, actionList);
}
for(i=0; i<bagList.length; i++){
if(bagList[i][0] != null)
drawButton(bagList[i][1], bagList[i][0].icon, i, bagList);
else
checkRectHover(bagList[i][1], null, i, bagList);
}
getKeyInput();
if(dragMode) drawDragIcon();
}
function drawButton(rect, image, index, pointer){
var drawIcon : boolean = true;
if(clickInfo.length > 0)
if(clickInfo[0] == rect dragMode)
drawIcon = false;
if(drawIcon)
GUI.DrawTexture(rect, image);
checkRectHover(rect, image, index, pointer);
}
function checkRectHover(rect, image, index, pointer){
if(rect.Contains(getMousePos())){
hoverInfo.Push(rect);
hoverInfo.Push(image);
hoverInfo.Push(index);
hoverInfo.Push(pointer[index][0]);
hoverInfo.Push(pointer);
hoverInfo.Push(getMousePos());
if(pointer[index][0]){
if(!Input.GetMouseButton(0))
GUI.DrawTexture(rect, highlight);
else
GUI.DrawTexture(rect, activated);
}
}
}
function storeMouseLoc(){
if(hoverInfo.length > 0 hoverInfo[3] != null)
clickInfo = hoverInfo;
}
function dragCheck(){
if(clickInfo.length > 0)
if((getMousePos() - clickInfo[5]).sqrMagnitude > 60) dragMode = true;
}
function compareMouseLoc(){
if(hoverInfo.length > 0 clickInfo.length > 0){
if(hoverInfo[2] == clickInfo[2] checkReq(clickInfo[4][hoverInfo[2]][0].type, hoverInfo[4][hoverInfo[2]][2]) !dragMode){
if(clickInfo[4][clickInfo[2]][0]){
clickInfo[4][clickInfo[2]][0].use();
}
}
if(hoverInfo[2] == clickInfo[2] hoverInfo[4] == clickInfo[4] dragMode){
}
if(hoverInfo[4][hoverInfo[2]][0]){
var clickedItemReq = checkReq(clickInfo[4][clickInfo[2]][0].type, hoverInfo[4][hoverInfo[2]][2]);
var hoverItemReq = checkReq(hoverInfo[4][hoverInfo[2]][0].type, clickInfo[4][clickInfo[2]][2]);
if(clickedItemReq hoverItemReq){
var clickSwap = clickInfo[4][clickInfo[2]][0];
var hoverSwap = hoverInfo[4][hoverInfo[2]][0];
clickInfo[4][clickInfo[2]][0] = hoverSwap;
hoverInfo[4][hoverInfo[2]][0] = clickSwap;
}
}
else{
if(checkReq(clickInfo[4][clickInfo[2]][0].type, hoverInfo[4][hoverInfo[2]][2])){
hoverInfo[4][hoverInfo[2]][0] = clickInfo[4][clickInfo[2]][0];
clickInfo[4][clickInfo[2]][0] = null;
}
}
}
else if(hoverInfo.length == 0 clickInfo.length > 0){
clickInfo[4][clickInfo[2]][0] = null;
}
clickInfo = new Array();
dragMode = false;
}
function getHoverRect(hoverRect){
hoverRect.x += 1;
hoverRect.y += 1;
hoverRect.width -= 1;
hoverRect.height -= 1;
return hoverRect;
}
function getNextSlot(list){
var slotFound = -1;
for(i=0; i<list.length; i++){
if(!list[i][0]){
slotFound = i;
break;
}
}
return slotFound;
}
function checkReq(item, list){
var result = false;
for(i=0; i<list.length; i++){
if(item == list[i]){
result = true;
break;
}
}
return result;
}
function drawDragIcon(){
var pos = getMousePos();
GUI.DrawTexture(Rect(pos.x - 15, pos.y - 15, 24, 24), clickInfo[1]);
}
function getMousePos(){
var pos : Vector2 = new Vector2(Input.mousePosition.x, (Screen.height - Input.mousePosition.y));
return pos;
}
function getKeyInput(){
for(i=0; i<actionList.length; i++){
if(Input.GetKey(actionList[i][3]) actionList[i][0]){
GUI.DrawTexture(actionList[i][1], activated);
}
else if(Input.GetKeyUp(actionList[i][3]) actionList[i][0]){
actionList[i][0].use();
}
}
}
The Classes :
class classSnowOrb{
var type = "action";
static var icon : Texture2D = Resources.Load("GUI/ActionbarGUI/action-icon-1");
static var description : String = "Unleashes the fury of Snow!";
function use(){
Debug.Log(description);
}
}
class classFireBall{
var type = "action";
static var icon : Texture2D = Resources.Load("GUI/ActionbarGUI/action-icon-2");
static var description : String = "Fire Ball Attack.";
function use(){
Debug.Log(description);
}
}
Here is the errors :
NullReferenceException
UnityEngine.Texture.get_width () (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/Graphics.cs:900)
UnityEngine.GUI.DrawTexture (Rect position, UnityEngine.Texture image, ScaleMode scaleMode, Boolean alphaBlend, Single imageAspect) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/GUI.cs:121)
UnityEngine.GUI.DrawTexture (Rect position, UnityEngine.Texture image) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/GUI.cs:116)
HUD.OnGUI () (at Assets/HUD.js:71)
NullReferenceException: Object reference not set to an instance of an object
UnityEngine.GUI.DrawTexture (Rect position, UnityEngine.Texture image, ScaleMode scaleMode, Boolean alphaBlend, Single imageAspect) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/GUI.cs:121)
UnityEngine.GUI.DrawTexture (Rect position, UnityEngine.Texture image) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/GUI.cs:116)
HUD.OnGUI () (at Assets/HUD.js:71)
Please Help if you can !
Thankyou
Chris