Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ignotuscaligo · Mar 08, 2012 at 09:18 PM · arrayfunctionclasscalling

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ignotuscaligo · Mar 08, 2012 at 10:22 PM 0
Share

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.

avatar image Berenger · Mar 08, 2012 at 10:50 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges