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 BTStone · Mar 02, 2012 at 06:40 PM · c#arraytutorial

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)

Comment
Add comment · Show 1
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 Twinny · Jul 04, 2013 at 03:45 AM 0
Share

i need some help with this as well. i've tried everything and loopSprites is appearing in the inspecter

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

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
avatar image
0

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 {
 ..."
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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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