- Home /
Unity 3 GD HotShot-Tutorial Problem with an C#-Array
Hey Folks, i've got a Problem. I bought a book Unity 3 Game Development HotShot, which is pretty awesome. But i have a problem. There is a task, where i have to make a little 2D project. So one has to make a SpriteManager class, this here:
using UnityEngine;
using System.Collections;
public class CharacterController_2D : MonoBehaviour {
public float f_speed = 5.0f;
public Manager[] loopSprites;
public int in_direction;
// Use this for initialization
void Start ()
{
in_direction = 1;
for(int i = 0; i<loopSprites.Length; i++)
{
loopSprites[1].init();
}
Camera.main.transform.position = new Vector3(transform.position.x, transform.position.y, Camera.main.transform.position.z);
}
// Update is called once per frame
void Update ()
{
if(Input.GetButton("Horizontal"))
{
in_direction = Input.GetAxis("Horizontal") < 0 ? -1:1;
rigidbody.velocity = new Vector3((in_direction*f_speed), rigidbody.velocity.y,0);
// Reset Stay animation frame back to the first frame
loopSprites[0].resetFrame();
//Update Walking animation
loopSprites[1].updateAnimation(in_direction,renderer.material);
}
}
public void LateUpdate()
{
//Update Main Camera
Camera.main.transform.position = new Vector3(transform.position.x, transform.position.y, Camera.main.transform.position.z);
}
public class Manager
{
public Texture2D spriteTextur;
public int in_framePerSec;
public int in_gridX, in_gridY;
private float f_timePercent;
private float f_nextTime;
private float f_gridX, f_gridY;
private int in_curFrame;
public void init()
{
f_timePercent = 1.0f/(float)in_framePerSec;
f_nextTime = f_timePercent;
f_gridX = 1.0f/(float)in_gridX;
f_gridY = 1.0f/(float)in_gridY;
in_curFrame = 1;
}
public void updateAnimation(int _direction, Material _material)
{
// Update Material
_material.mainTexture = spriteTextur;
//Update Frame by Time
if(Time.time>f_nextTime)
{
f_nextTime = Time.time + f_timePercent;
in_curFrame++;
if(in_curFrame > in_framePerSec)
{
in_curFrame = 1;
}
}
_material.mainTextureScale = new Vector2(_direction*f_gridX, f_gridY);
int in_col = 0;
if(in_gridY>1)
{
in_col = (int)Mathf.Ceil(in_curFrame/in_gridX);
}
if(_direction == 1)
{
_material.mainTextureOffset = new Vector2(((in_curFrame)%in_gridX) *f_gridX, in_col*f_gridY);
}
else
{
//Flip Texture
_material.mainTextureOffset = new Vector2(((in_gridX+(in_curFrame)%in_gridX))*f_gridX, in_col*f_gridY);
}
}
public void resetFrame()
{
in_curFrame = 1;
}
}
}
So, then i drag'n'dopped the Script to my Player-Object and looked up at the inspector. But at the ScriptComponent i just can see these two variables
F_Speed 5 In_Direction 0
No SpriteManager array called loopSprites which i can work with in the inspector. The Tutorialbook is telling me to go to "Loop Sprites" and to set the Size to 2 and then setting the Elements in a certain way.
Can someone please tell me, what did i do wrong. It must be somewhere in the Code.
I have to mention: The Tutorial-Code is written in Java-Script, i "translated" it into C#, cause i am more familiar with C# than JavaScript (please no wars about which language is better)
i need some help with this as well. i've tried everything and loopSprites is appearing in the inspecter
Answer by BTStone · Mar 02, 2012 at 10:07 PM
Alright, i got the problem. I forget to put before making the other SpriteManager class a
[System.Serializable]
Before the class itself. At least it worked with that. I hope it's the right solution, if anyone got some other ideas, suggestions, solutions etc. i'll be looking forward to read them :)
Answer by zayeds · Jun 24, 2013 at 12:28 AM
I experienced the same problem. Is there any chance you can explain what you did to make it work because I tried doing what you said and it didn't work for me.
This is what I did:
"...
public function LateUpdate() : void {
//Update Main Camera
Camera.main.transform.position = new Vector3(transform.position.x, transform
position.y, Camera.main.transform.position.z);
}
[System.Serializable]
class SpriteManager {
..."
Your answer
Follow this Question
Related Questions
C# SetActive GameObject Array 2 Answers
Array iteration problem 1 Answer
Array Of Bools 4 Answers
C# Children and SubChildren Null Reference Exception 1 Answer
Distribute terrain in zones 3 Answers