- Home /
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:
What the problem is.
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.
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.)
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.
Answer by Lo0NuhtiK · Apr 30, 2012 at 05:21 AM
[System.Serializable]
public class SpriteManager{
Your answer
Follow this Question
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