Please Help Me Out

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

In the inspector, have you assigned textures to all of the following fields:

private var icon1 : Texture2D;
private var icon2 : Texture2D;
private var highlight : Texture2D;
private var activated : Texture2D;
private var slotBG : Texture2D;

?

(Based on the line number, I’d check slotBG in particular.)

Yes i have checked. If you have teamviewer and want to
check if i missed anything please freely send me a PM.

Add

if (icon1 == null) {
    Debug.Log ("icon1 is null");
}

if (icon2 == null) {
    Debug.Log ("icon2 is null");
}

if (highlight == null) {
    Debug.Log ("highlight is null");
}

if (activated == null) {
    Debug.Log ("activated is null");
}

if (slotBG == null) {
    Debug.Log ("slotBG is null");
}

just to be sure. From the errors it seems highly likely that one of these is not being assigned correctly.

By making the Texture2D variables public, assigning them in the inspector and commenting out the awake block, the code works fine for me. So you might want to check the paths for your Resources.Load, seems like at least one of them is failing.

After reading the documentation on Resources.Load, it seems you can only load assets this way that are in insude of a Resources folder in your project. If you really need to load them this way, you’ll need to move them appropiately.

Otherwise your better off just linking them manually in the editor. Change your declarations for the Texture2D’s to the code below and remove the Awake() function part.

var icon1 : Texture2D;
var icon2 : Texture2D;
var highlight : Texture2D;
var activated : Texture2D;
var slotBG : Texture2D;

Now you should be able to see them on the gameobject in your scene that has your script on it. Simply link the correct images in via the editor and it should work fine.

Hope this helps

Thanks,

However it still doesn’t work.

Error Code:

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.drawButton (System.Object rect, System.Object image, System.Object index, System.Object pointer) (at Assets/HUD.js:106)
HUD.OnGUI () (at Assets/HUD.js:81)

Well the error has moved down, now it’s having an error in you drawButton function again relating to null texture it seems… progress maybe!

Do you have any other scripts running that could be changing the images? Did you comment out or delete your old Awake() function? If you select the game object with the script on when its playing, do any of the texture variables appear as None, Empty or Null?

When i press the gui.buttons to make the icons appear in the action bar, i get a another null. It saying something about can not find the image .

You have the same Reource.Load issue in your classes, so the icons for those will be coming back null as well

Yes but when i try and do what you said, I will have to attach them files to the object has well. However i get error saying something about the script can not be added to a object.

Indeed your right. Here’s one way to solve it -

I’ve edited your spell classes to now take the Icon as part of the constructor:

class classSnowOrb{
    
    var type = "action";   

    static var icon : Texture2D;
    static var description : String = "Unleashes the fury of Snow!";

	function classSnowOrb(snowicon : Texture2D){
		if (icon != null)
			return;
			
		icon = snowicon;
	}

    function use(){
        Debug.Log(description);  
    }
}

class classFireBall{
   
    var type = "action";

    static var icon : Texture2D;
    static var description : String = "Fire Ball Attack.";

	function classFireBall(fireicon : Texture2D){
		if (icon != null)
			return;
			
		icon = fireicon;
	}

    function use(){
        Debug.Log(description);   
    }
}

Your action bar script needs to now pass the classes the right Icon when they are created. This is needed at the top:

var Icons : Texture2D[];

This will give you an empty array. Declare the size in the inspector (2 is all you need for now), then link in the images as before. Your class calls in your actionbar script also want to be changed. From this:

new classSnowOrb();

to

new classSnowOrb(Icons[0]);

Similarly, the classFireOrb one needs changing to using Icon[1] from the array. Seems to all work fine now for me :slight_smile:

Hope this helps

Thankyou it works fine now.