- Home /
Calling functions from a class - Curious issue
hello all
So the issue I've run into is that I have an object that I've defined in a .js as a class. Here's a dummy class to describe this issue: class Box { public var contents : float = 1.0; function Box() { } function GetContents() { return(contents); } } Now, in another script, I have an array of these objects:
public var boxes : Box[] = new Box[10]; //10 is arbitrary
During the Update() cycle of this script containing the array, I cycle through each "Box" and call this function:
function Update()
{
for(var i = 0; i < boxes.length; i++)
{
var tmpBox : Box = boxes[i];
print(tmpBox.GetContents());
}
}
Most of the time when I do this, everything functions normally in terms of actually executing this function. My issue now is that while I'm asking Unity to call that function, the program never goes into it.
Consider: function Update() { for(var i = 0; i < boxes.length; i++) { var tmpBox : Box = boxes[i]; print("getting the contents now..."); //added this line print(tmpBox.GetContents()); print("you should see contents"); //and this one } } I SHOULD see this as my output:
"getting the contents now..."
1.0
"you should see contents"
But what I actually get is just:
"getting the contents now..."
"you should see contents"
The function "GetContents()" never runs.
Anyone have any ideas as to what this could be? Any clarifications needed about the problem?
EDIT:: One other note I forgot, I can access the variables in said class, just not the functions. I can change box.contents, but box.GetContents() doesn't execute
Answer by Berenger · Mar 08, 2012 at 09:33 PM
I tried your code, and I get the correct output, "getting the contents now..." 1.0 "you should see contents" (in three lines). Are you sure your not doing something else screwing it up ? (Can't imagine what ...)
I'm honestly not sure what is wrong. I've scoured the code looking for typos and what not, or maybe some weird null reference. One thing that is really odd is that I can access the variables within the class, like, if I did print(boxes[0].contents);, I would see 1.0.
One major note is that this isn't the actual function, I was just using this to get my point across.
Try just that, it works for me
#pragma strict
public class BacASableJS extends $$anonymous$$onoBehaviour { public class Box { public var contents : float = 1.0; function GetContents(){ return(contents); } }
public var boxes : Box[] = new Box[10]; //10 is arbitrary
function Start()
{
for(var i = 0; i < boxes.length; i++)
boxes[i] = new Box();
}
/*function Update()
{
for(var i = 0; i < boxes.length; i++)
{
var tmpBox : Box = boxes[i];
print(tmpBox.GetContents());
}
}*/
function Update()
{
for(var i = 0; i < boxes.length; i++)
{
var tmpBox : Box = boxes[i];
print("getting the contents now..."); //added this line
print(tmpBox.GetContents());
print("you should see contents"); //and this one
}
}
}
Your answer
Follow this Question
Related Questions
I need "MainClass" 1 Answer
WARNING : Assignment to temporary. 1 Answer
Script wont read array 2 Answers
JSON missing arrays 1 Answer
Reassigning an Array causing problems 2 Answers