x


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

more ▼

asked Mar 08 '12 at 09:18 PM

ignotuscaligo gravatar image

ignotuscaligo
18 4 9 11

(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

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 ...)

more ▼

answered Mar 08 '12 at 09:33 PM

Berenger gravatar image

Berenger
11k 12 19 53

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.

Mar 08 '12 at 10:22 PM ignotuscaligo

Try just that, it works for me

#pragma strict

public class BacASableJS extends MonoBehaviour 
{
	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
	    }
	}	
}
Mar 08 '12 at 10:50 PM Berenger
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1363
x478
x337
x21

asked: Mar 08 '12 at 09:18 PM

Seen: 453 times

Last Updated: Mar 08 '12 at 10:50 PM