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