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
2
Question by Endante · Apr 30, 2012 at 04:41 AM · c#javascripttypeconversion

C# conversion of JavaScript, problems with nested class as a custom type

I am trying to work through a book on Unity, "Unity 3 Game Development Hotshot"- Packt Publishing, and all the examples are in Javascript. I wish to use C# so I decided to covert as I go.

I am having issues with one of the early scripts not behaving as expected in the Inspector when translated to C#, and I was hoping the community might be able to help give me a pointer on:

  1. What the problem is.

  2. Where I can find the reference that might have allowed me to answer this myself. I was unable to locate any help in the manuals.

What is going on, I am creating a class as a custom type, then creating an array of that type.

When the script with the array is added to a GameObject the Inspector is suposed to allow me to enter the array size, then present me with the required number of elements in which I can access and set the public variables for each index of the array. In javascript it is doing that just fine. In C# I set the array size and it presents me with a set of elements (one for each array index) but the public variables of the custom class are not avaialable.

If I copy the custom class out and make it it's own script, then attach that script to a GameObject, the public variables are accessable and I can set them as expected. I suspect the issue is with how I am creating either the aray, or the customer object type, but am not sure where my error is. (The one thing I do know is it is something I am overlooking.)


Javascript working (edited some to avoid violating PacktPub's copyright)

 public var loopSprites : SpriteManager[];

 public function Start() : void {

 //Initialization Sprite Manager
 for (var i : int = 0; i < loopSprites.length; i++) {
     loopSprites[i].init();
 }
 //aditional code here... not relavant really
 }

 class SpriteManager {
 public var spriteTexture : Texture2D; //Set Texture use for a loop animation such as walking, stay, etc.
 public var in_framePerSec : int; //Get frame per sec to calculate time
 public var in_gridX : int; //Get max number of Horizontal images
 public var in_gridY : int; //Get max number of Vertical images
 private var f_timePercent : float;
 private var f_nextTime : float; //Update time by using frame persecond
 private var f_gridX : float;
 private var f_gridY : float;
 private var in_curFrame : int;
 
 public function init () : void {
 f_timePercent = 1.0/in_framePerSec;
 f_nextTime = f_timePercent; //Update time by using frame persecond
 f_gridX = 1.0/in_gridX;
 f_gridY = 1.0/in_gridY;
 in_curFrame = 1;
 }
 
 }



C# -Does not work

 using UnityEngine;
 using System.Collections;

 public class test2 : MonoBehaviour {
 public SpriteManager[] loopSprites;
 


 void Start () {
     //initialization of Sprite Manager
     for (int i=0; i<loopSprites.Length; i++){
         loopSprites[i].init();
     }
     // Addition code... Also not really relevant
 }
 
 public class SpriteManager : MonoBehaviour {
 
     public Texture2D spriteTexture; //Set Texture for the sprite animation
     public int framesPerSec; //Get Frames Per Second to calculate time
     public int gridXMax; //max number of horizontal images
     public int gridYMax; //max number of vertical images
     float timePercent;
     float nextTime; //next update time based on framesPerSec
     float gridXOffset;
     float gridYOffset;
     int currentFrame;
 
     public void init () {
         timePercent=1.0f/framesPerSec;
         nextTime=timePercent;
         gridXOffset= 1.0f/gridXMax;
         gridYOffset=1.0f/gridYMax;
         currentFrame=1;
     }
 
 }
 }



Any pointers would be greatly appreciated. Also Please forgive me if this is a duplicated post (or very similar), these forums are substantial, and I was not able to find any relevant topic. This may have been due to not know what to look for.

Comment
Add comment · Show 2
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 Seth-Bergman · Apr 30, 2012 at 05:03 AM 0
Share

maybe try: public Sprite$$anonymous$$anager[] loopSprites; loopSprites = new Sprite$$anonymous$$anager[size];

something like that

(I think leaving out the size will work as you desire.)

avatar image Endante · May 01, 2012 at 02:38 AM 0
Share

Thanks Seth, But $$anonymous$$onoDevelop shows an error and Unity reports Array creation must have array size or array initializer. I appreciate the atempt very much.

1 Reply

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Lo0NuhtiK · Apr 30, 2012 at 05:21 AM

[System.Serializable]
public class SpriteManager{

Comment
Add comment · 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

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

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Converting a JS script into C# - hit a wall with arrays... 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

I need help with a script(brauche hilfe mit einen Script) 0 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