- Home /
I am having an issue with returning an array
I am having an issue with rturning an array from a function and storing that in a var. the error is.
NullReferenceException: Object reference not set to an instance of an object screengui.SkillBox (Int32 x, Int32 y) (at Assets/screengui.js:26) screengui.OnGUI () (at Assets/screengui.js:15)
the following is my code.
// from GUIData
function GetData( prof:int, branch:int, level:int) : Object{
...
var data = [ "", 0, "Novice Brawler", 88, 15 ];
return data;
...
}
var data:Object[] = []; // line 25
data = dataset.GetData( 0, 0, 0 ); //line 26
I have tried type casting data and the GetData function but it still errors out. This should be pretty simple but I have no idea why this is happening. All I want is to pass an array to a var. The Data var that is returned will be different based on the input of the function. Can someone tell me what I am missing?
Have you created an object "dataset" which has the class function "GetData" before?
Regards, BPR
Type casting the varibles did not fix the issues,
Assets/screengui.js(25,18): BCE0018: The name 'object' does not denote a valid type ('not found'). Did you mean 'UnityEditorInternal.Object$$anonymous$$emoryInfo'?
not putting any type casting does not produce any compile errors however,it does produce a run time error
NullReferenceException: Object reference not set to an instance of an object screengui.SkillBox (Int32 x, Int32 y) (at Assets/screengui.js:25) screengui.OnGUI () (at Assets/screengui.js:15)
the current code is
function GetData( prof:int, branch:int, level:int) {
var data = [ "", 0, "Novice Brawler", 88, 15 ];
return data;
}
var dataset:GUIData; //line 24
var data = dataset.GetData( 0, 0, 0 ); //line26
@BPR No, I have not used this class before. This is the first time I have tried this kind of thing in javascript, I mainly use java so there is abit of a learning curve.
Answer by gump74 · Jun 21, 2013 at 06:19 AM
I was able to figure it out. I tried moving GetData into the same file and calling it from there, that worked just fine. After recoding it back to separate files I was still getting the "NullReferenceException", I figured the dataset varible was not being properly instantiated. I read http://answers.unity3d.com/questions/24515/How-to-create-a-new-class.html; I found that to be quite helpfull. If there is anyone out there that runs into the same problem I did I will attach my new code so that others may find it useful.
class GUIData {
function GUIData() {
}
function GetData( prof:int, branch:int, level:int) {
var data = [ "", 0, "Novice Brawler", 88, 15 ];
return data;
}
}
var dataset:GUIData = new GUIData();
var data = dataset.GetData( 0, 0, 0 );
Answer by Jessy · Jun 20, 2013 at 07:53 PM
Object with a capital letter is UnityEngine.Object; none of the stuff in your array is that. Use a lowercase o.
Also, you're returning Object and not Object[].
I really doubt you want to use that code, but those are your problems.
Your answer
![](https://koobas.hobune.stream/wayback/20220613104314im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Need help with my script 0 Answers
Limit on GUI Components? 0 Answers
Setting Scroll View Width GUILayout 1 Answer
How To Make Ammo & Realod for Gun & Spark for Gun ? 0 Answers
How Do I Create A Nested Function With Unity JavaScript? 1 Answer